我们有4个字符(A,B,C和D)的序列,映射到数字形式1到n。 我们将组件定义为:
Component(k) : A {cell[k]} if Color(left_k) = Color(k) then A <-- A U Component(left_k) if Color(right_k) = Color(k) then A <-- A U Component(left_k) return A有3种类型的操作(列表中的数字表示输入):
通过给索引它应该删除该索引中的组件(映射到字符的数字是固定的)
示例:AABBBDA是字符串。 如果指数为3则应返回AADA
通过给索引它应该根据该索引上的组件旋转字符串(映射到字符的数字是固定的)
示例:AABBBDA是字符串。 如果index为3,则应返回DABBBAA
它应该打印字符串。
输入如下:
1 2 --> first operation with index=2 2 3 --> second operation with index=3 3 --> third operation这是一项任务,很乐意得到帮助。
这是我到目前为止所尝试的:
public static void main(String[] args) { int numberOfOps; String[] print = new String[30]; List list = new List(); Scanner input = new Scanner(System.in); int count = input.nextInt(); String colors = new String(); colors = input.next(); for(int i = 0; i < count; i++) { list.add(colors.charAt(i)); } numberOfOps = input.nextInt(); list.printElement(); for (int i = 0; i < numberOfOps; i++) { int op = input.nextInt(); if(op == 1) { int index = input.nextInt(); char c = list.item[index]; int temp = index; int prevIndex = index; int nexIndex = index; if(index != 0) { while (list.item[--index] == c) { prevIndex--; } while (list.item[++temp] == c) { nexIndex++; } list.setNext(prevIndex-1, nexIndex+1); } else { while (list.item[++temp] == c) { nexIndex++; } list.setNext(prevIndex, nexIndex+1); } } if(op == 2) { int index = input.nextInt(); } if(op == 3) { print[i] = list.printElement(); } } }这是我的List类:
public class List { // reference to linked list of items public static final int MAX_LIST = 20; public static final int NULL = -1; public char item[] = new char[MAX_LIST]; // data public int avail; public int next[] = new int[MAX_LIST]; // pointer to next item private int numItems; // number of items in list public List() { int index; for (index = 0; index < MAX_LIST-1; index++) next[index] = index + 1; next[MAX_LIST-1] = NULL; numItems = 0; avail = 0; } // end default constructor public void add(char e) { item[avail] = e; avail = next[avail]; numItems++; } public String printElement() { String temp = null; int index = 0; while(index<avail) { temp += item[index]; System.out.println(item[index]); index = next[index]; } return temp; } public int size() { return numItems; } public void setNext(int i, int value) { next[i] = value; } }如果你测试它,你会得到它,它有很多问题,例如,我不知道做旋转操作,并且在中间组件被移除时连接两个组件有问题。
we have sequence of 4 characters (A,B,C and D)that map to numbers form 1 to n. we define components to be:
Component(k) : A {cell[k]} if Color(left_k) = Color(k) then A <-- A U Component(left_k) if Color(right_k) = Color(k) then A <-- A U Component(left_k) return Athere is 3 types of operations(the numbers in list indicate the input):
by giving index it should remove the component in that index(the numbers mapping to characters are fixed)
example : AABBBDA is the string. if index is 3 it should return AADA
by giving index it should rotate the string based on the component on that index(the numbers mapping to characters are fixed)
example : AABBBDA is the string. if index is 3 it should return DABBBAA
it should print the string.
inputs are like:
1 2 --> first operation with index=2 2 3 --> second operation with index=3 3 --> third operationIt's an assignment, happy to get help.
this is what i've tried so far:
public static void main(String[] args) { int numberOfOps; String[] print = new String[30]; List list = new List(); Scanner input = new Scanner(System.in); int count = input.nextInt(); String colors = new String(); colors = input.next(); for(int i = 0; i < count; i++) { list.add(colors.charAt(i)); } numberOfOps = input.nextInt(); list.printElement(); for (int i = 0; i < numberOfOps; i++) { int op = input.nextInt(); if(op == 1) { int index = input.nextInt(); char c = list.item[index]; int temp = index; int prevIndex = index; int nexIndex = index; if(index != 0) { while (list.item[--index] == c) { prevIndex--; } while (list.item[++temp] == c) { nexIndex++; } list.setNext(prevIndex-1, nexIndex+1); } else { while (list.item[++temp] == c) { nexIndex++; } list.setNext(prevIndex, nexIndex+1); } } if(op == 2) { int index = input.nextInt(); } if(op == 3) { print[i] = list.printElement(); } } }here is my List class:
public class List { // reference to linked list of items public static final int MAX_LIST = 20; public static final int NULL = -1; public char item[] = new char[MAX_LIST]; // data public int avail; public int next[] = new int[MAX_LIST]; // pointer to next item private int numItems; // number of items in list public List() { int index; for (index = 0; index < MAX_LIST-1; index++) next[index] = index + 1; next[MAX_LIST-1] = NULL; numItems = 0; avail = 0; } // end default constructor public void add(char e) { item[avail] = e; avail = next[avail]; numItems++; } public String printElement() { String temp = null; int index = 0; while(index<avail) { temp += item[index]; System.out.println(item[index]); index = next[index]; } return temp; } public int size() { return numItems; } public void setNext(int i, int value) { next[i] = value; } }if you test it you'll get, it has lots of problems, such as, I have no idea to do the rotate operation, and it has problem with connecting two components when the middle component has been removed.
最满意答案
这是一个难以回答的问题,因为没有正确说明要求。
例如,第一组伪代码不能清楚地表明A是集合,多集还是列表。 符号(使用花括号和U(联合?))似乎说set ...但输出似乎是一个列表。 或者它应该是数据结构的模式?
甚至输入也没有明确描述。
但是把它放在一边,仍然有一些(希望)有用的建议。
确保>>你<<了解要求。 (我想这个作业的真正要求比这更明确,细节已经“在翻译中丢失”。)
我实际上会使用数组列表(或StringBuilder)而不是链接列表。 (但正确实现的链表...实现List API ...会起作用。)
但无论您选择何种数据结构,从头开始实施它都没有意义......除非您特别要求这样做。 Java标准库中有很好的列表类。 你应该重复使用它们......而不是试图重新发明轮子(做坏事)。
如果您需要实现自己的数据结构类型,那么您当前的尝试是一团糟。 它看起来像是一个数组列表和一个链表之间的混合......并且不会成功。 (例如,一个不错的数组列表实现不需要MAX_LIST ,也没有next指针/索引。链接列表里面没有任何数组。)
This is a difficult question to answer, because the requirements are not properly stated.
For example the first bunch of pseudo-code does not make it clear whether A is a set, a multi-set or a list. The notation (use of curly brackets, and U (union?)) seems to say set ... but the output seems to be a list. Or maybe it is supposed to be a schema for a data structure??
And even the inputs are not clearly described.
But putting that on one side, there is still room for some (hopefully) helpful advice.
Make sure that >>you<< understand the requirements. (I imagine that the real requirements for the assignment are better stated than this, and the details have been "lost in translation".)
I would actually use an array list (or a StringBuilder) rather than a linked list for this. (But a properly implemented linked list ... implementing the List API ... would work.)
But whatever data structure you chose, there is no point in implementing it from scratch ... unless you are specifically required to do that. There are perfectly good list classes in the Java standard libraries. You should reuse them ... rather than attempting to reinvent the wheel (and doing a bad job).
If you are required to implement your own data structure type, then your current attempt is a mess. It looks like a hybrid between an array list and a linked list ... and doesn't succeed in being either. (For example, a decent array list implementation does not need a MAX_LIST, and doesn't have next pointers / indexes. And a linked list does not have any arrays inside it.)
使用数据结构在O(n)中解决这个问题(Using a data structure to solve this in O(n))我们有4个字符(A,B,C和D)的序列,映射到数字形式1到n。 我们将组件定义为:
Component(k) : A {cell[k]} if Color(left_k) = Color(k) then A <-- A U Component(left_k) if Color(right_k) = Color(k) then A <-- A U Component(left_k) return A有3种类型的操作(列表中的数字表示输入):
通过给索引它应该删除该索引中的组件(映射到字符的数字是固定的)
示例:AABBBDA是字符串。 如果指数为3则应返回AADA
通过给索引它应该根据该索引上的组件旋转字符串(映射到字符的数字是固定的)
示例:AABBBDA是字符串。 如果index为3,则应返回DABBBAA
它应该打印字符串。
输入如下:
1 2 --> first operation with index=2 2 3 --> second operation with index=3 3 --> third operation这是一项任务,很乐意得到帮助。
这是我到目前为止所尝试的:
public static void main(String[] args) { int numberOfOps; String[] print = new String[30]; List list = new List(); Scanner input = new Scanner(System.in); int count = input.nextInt(); String colors = new String(); colors = input.next(); for(int i = 0; i < count; i++) { list.add(colors.charAt(i)); } numberOfOps = input.nextInt(); list.printElement(); for (int i = 0; i < numberOfOps; i++) { int op = input.nextInt(); if(op == 1) { int index = input.nextInt(); char c = list.item[index]; int temp = index; int prevIndex = index; int nexIndex = index; if(index != 0) { while (list.item[--index] == c) { prevIndex--; } while (list.item[++temp] == c) { nexIndex++; } list.setNext(prevIndex-1, nexIndex+1); } else { while (list.item[++temp] == c) { nexIndex++; } list.setNext(prevIndex, nexIndex+1); } } if(op == 2) { int index = input.nextInt(); } if(op == 3) { print[i] = list.printElement(); } } }这是我的List类:
public class List { // reference to linked list of items public static final int MAX_LIST = 20; public static final int NULL = -1; public char item[] = new char[MAX_LIST]; // data public int avail; public int next[] = new int[MAX_LIST]; // pointer to next item private int numItems; // number of items in list public List() { int index; for (index = 0; index < MAX_LIST-1; index++) next[index] = index + 1; next[MAX_LIST-1] = NULL; numItems = 0; avail = 0; } // end default constructor public void add(char e) { item[avail] = e; avail = next[avail]; numItems++; } public String printElement() { String temp = null; int index = 0; while(index<avail) { temp += item[index]; System.out.println(item[index]); index = next[index]; } return temp; } public int size() { return numItems; } public void setNext(int i, int value) { next[i] = value; } }如果你测试它,你会得到它,它有很多问题,例如,我不知道做旋转操作,并且在中间组件被移除时连接两个组件有问题。
we have sequence of 4 characters (A,B,C and D)that map to numbers form 1 to n. we define components to be:
Component(k) : A {cell[k]} if Color(left_k) = Color(k) then A <-- A U Component(left_k) if Color(right_k) = Color(k) then A <-- A U Component(left_k) return Athere is 3 types of operations(the numbers in list indicate the input):
by giving index it should remove the component in that index(the numbers mapping to characters are fixed)
example : AABBBDA is the string. if index is 3 it should return AADA
by giving index it should rotate the string based on the component on that index(the numbers mapping to characters are fixed)
example : AABBBDA is the string. if index is 3 it should return DABBBAA
it should print the string.
inputs are like:
1 2 --> first operation with index=2 2 3 --> second operation with index=3 3 --> third operationIt's an assignment, happy to get help.
this is what i've tried so far:
public static void main(String[] args) { int numberOfOps; String[] print = new String[30]; List list = new List(); Scanner input = new Scanner(System.in); int count = input.nextInt(); String colors = new String(); colors = input.next(); for(int i = 0; i < count; i++) { list.add(colors.charAt(i)); } numberOfOps = input.nextInt(); list.printElement(); for (int i = 0; i < numberOfOps; i++) { int op = input.nextInt(); if(op == 1) { int index = input.nextInt(); char c = list.item[index]; int temp = index; int prevIndex = index; int nexIndex = index; if(index != 0) { while (list.item[--index] == c) { prevIndex--; } while (list.item[++temp] == c) { nexIndex++; } list.setNext(prevIndex-1, nexIndex+1); } else { while (list.item[++temp] == c) { nexIndex++; } list.setNext(prevIndex, nexIndex+1); } } if(op == 2) { int index = input.nextInt(); } if(op == 3) { print[i] = list.printElement(); } } }here is my List class:
public class List { // reference to linked list of items public static final int MAX_LIST = 20; public static final int NULL = -1; public char item[] = new char[MAX_LIST]; // data public int avail; public int next[] = new int[MAX_LIST]; // pointer to next item private int numItems; // number of items in list public List() { int index; for (index = 0; index < MAX_LIST-1; index++) next[index] = index + 1; next[MAX_LIST-1] = NULL; numItems = 0; avail = 0; } // end default constructor public void add(char e) { item[avail] = e; avail = next[avail]; numItems++; } public String printElement() { String temp = null; int index = 0; while(index<avail) { temp += item[index]; System.out.println(item[index]); index = next[index]; } return temp; } public int size() { return numItems; } public void setNext(int i, int value) { next[i] = value; } }if you test it you'll get, it has lots of problems, such as, I have no idea to do the rotate operation, and it has problem with connecting two components when the middle component has been removed.
最满意答案
这是一个难以回答的问题,因为没有正确说明要求。
例如,第一组伪代码不能清楚地表明A是集合,多集还是列表。 符号(使用花括号和U(联合?))似乎说set ...但输出似乎是一个列表。 或者它应该是数据结构的模式?
甚至输入也没有明确描述。
但是把它放在一边,仍然有一些(希望)有用的建议。
确保>>你<<了解要求。 (我想这个作业的真正要求比这更明确,细节已经“在翻译中丢失”。)
我实际上会使用数组列表(或StringBuilder)而不是链接列表。 (但正确实现的链表...实现List API ...会起作用。)
但无论您选择何种数据结构,从头开始实施它都没有意义......除非您特别要求这样做。 Java标准库中有很好的列表类。 你应该重复使用它们......而不是试图重新发明轮子(做坏事)。
如果您需要实现自己的数据结构类型,那么您当前的尝试是一团糟。 它看起来像是一个数组列表和一个链表之间的混合......并且不会成功。 (例如,一个不错的数组列表实现不需要MAX_LIST ,也没有next指针/索引。链接列表里面没有任何数组。)
This is a difficult question to answer, because the requirements are not properly stated.
For example the first bunch of pseudo-code does not make it clear whether A is a set, a multi-set or a list. The notation (use of curly brackets, and U (union?)) seems to say set ... but the output seems to be a list. Or maybe it is supposed to be a schema for a data structure??
And even the inputs are not clearly described.
But putting that on one side, there is still room for some (hopefully) helpful advice.
Make sure that >>you<< understand the requirements. (I imagine that the real requirements for the assignment are better stated than this, and the details have been "lost in translation".)
I would actually use an array list (or a StringBuilder) rather than a linked list for this. (But a properly implemented linked list ... implementing the List API ... would work.)
But whatever data structure you chose, there is no point in implementing it from scratch ... unless you are specifically required to do that. There are perfectly good list classes in the Java standard libraries. You should reuse them ... rather than attempting to reinvent the wheel (and doing a bad job).
If you are required to implement your own data structure type, then your current attempt is a mess. It looks like a hybrid between an array list and a linked list ... and doesn't succeed in being either. (For example, a decent array list implementation does not need a MAX_LIST, and doesn't have next pointers / indexes. And a linked list does not have any arrays inside it.)
发布评论