我在让这段代码在以逗号分隔的字符串上生成多个排列(排序)时遇到一些困难...我可以只做一个常规字符串,并让排列只对字母起作用,但在做时有点困难它用逗号分隔的单词...
为了让程序识别逗号,我使用了 StringTokenizer 方法,并将其放入 arrayList 中,但这实际上是我所得到的...问题又是我'我在排列每个单词时遇到困难...举一个例子,我将其发布在下面,然后将我的代码发布在下面...谢谢大家的帮助! ...并且通过排列我的意思是用逗号分隔的单词的顺序
例如,如果 BufferedReader 上的输入看起来像:
red,yellow
one,two,three
PrintWriter 上的输出应该看起来像:
red,yellow
yellow,red
one,two,three
one,three,two
two,one,three
two,three,one
three,one,two
three,two,one
请注意,输入总共有 3 行,包括“一,二,三”后有一个空行,而输出共有 11 行,其中“黄,红”后有一个空行,“三,二,一”后有两个空行。获得完全正确的格式至关重要,因为测试将是自动化的并且需要这种格式。另请注意,每个问题的输出行顺序并不重要。这意味着输出的前两行也可能是:
yellow,red
red,yellow
这是我到目前为止的代码...我已经注释掉了一些内容,所以不用担心这些部分
import java.io.*;
import java.util.*;
public class Solution
{
public static void run(BufferedReader in, PrintWriter out)
throws IOException
{
String str = new String(in.readLine());
while(!str.equalsIgnoreCase(""))
{
PermutationGenerator generator = new PermutationGenerator(str);
ArrayList<String> permutations = generator.getPermutations();
for(String str: permutations)
{
out.println(in.readLine());
}
out.println();
out.println();
}
out.flush();
}
public class PermutationGenerator
{
private String word;
public PermutationGenerator(String aWord)
{
word = aWord;
}
public ArrayList<String> getPermutations()
{
ArrayList<String> permutations = new ArrayList<String>();
//if(word.length() == 0)
//{
//permutations.add(word);
//return permutations;
//}
StringTokenizer tokenizer = new StringTokenizer(word,",");
while (tokenizer.hasMoreTokens())
{
permutations.add(word);
tokenizer.nextToken();
}
/*
for(int i = 0; i < word.length(); i++)
{
//String shorterWord = word.substring(0,i) + word.substring(i + 1);
PermutationGenerator shorterPermutationGenerator = new PermutationGenerator(word);
ArrayList<String> shorterWordPermutations =
shorterPermutationGenerator.getPermutations();
for(String s: shorterWordPermutations)
{
permutations.add(word.readLine(i)+ s);
}
}*/
//return permutations;
}
}
}
I'm having some difficulty having this code generate a number of permutations(orderings) on a string separated by commas ...I can do just a regular string and have the permutations work on just letters but it is a bit more difficult when doing it with words separated by the commas...
To have the program recognize the commas I used the StringTokenizer method and I'm putting it into an arrayList but that is really as far as I have gotten ...the problem again is I'm having trouble permuting each word...to give an example I'll post it below this and then my code below that...thank you for your help everyone! ...and by permutations I mean orderings of the words separated by the comma's
For example, if the input coming in on the BufferedReader looked like:
red,yellow
one,two,three
the output on the PrintWriter should look like:
red,yellow
yellow,red
one,two,three
one,three,two
two,one,three
two,three,one
three,one,two
three,two,one
Note that the input had 3 lines total, including the blank line after "one,two,three" while the output had 11 lines total, including one blank line after "yellow,red" and two blank lines after "three,two,one". It is vital that you get the format exactly correct as the testing will be automated and will require this format. Also note that the order of the output lines for each problem does not matter. This means the first two lines of the output could also have been:
yellow,red
red,yellow
here is the code I have so far ...I have commented some stuff out so don't worry about those parts
import java.io.*;
import java.util.*;
public class Solution
{
public static void run(BufferedReader in, PrintWriter out)
throws IOException
{
String str = new String(in.readLine());
while(!str.equalsIgnoreCase(""))
{
PermutationGenerator generator = new PermutationGenerator(str);
ArrayList<String> permutations = generator.getPermutations();
for(String str: permutations)
{
out.println(in.readLine());
}
out.println();
out.println();
}
out.flush();
}
public class PermutationGenerator
{
private String word;
public PermutationGenerator(String aWord)
{
word = aWord;
}
public ArrayList<String> getPermutations()
{
ArrayList<String> permutations = new ArrayList<String>();
//if(word.length() == 0)
//{
//permutations.add(word);
//return permutations;
//}
StringTokenizer tokenizer = new StringTokenizer(word,",");
while (tokenizer.hasMoreTokens())
{
permutations.add(word);
tokenizer.nextToken();
}
/*
for(int i = 0; i < word.length(); i++)
{
//String shorterWord = word.substring(0,i) + word.substring(i + 1);
PermutationGenerator shorterPermutationGenerator = new PermutationGenerator(word);
ArrayList<String> shorterWordPermutations =
shorterPermutationGenerator.getPermutations();
for(String s: shorterWordPermutations)
{
permutations.add(word.readLine(i)+ s);
}
}*/
//return permutations;
}
}
}
发布评论
评论(2)
您可以使用 String.split() ( http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#split(java.lang.String) )到将各个单词作为数组放入。您可以单独生成整数 {1..N} 的所有排列,其中 N 是单词数组的大小。然后使用数字排列作为索引遍历单词数组。
You can use String.split() ( http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#split(java.lang.String) ) to get the individual words into as an array. You can separately generate all the permutations on integers {1..N} where N is the size of the word array. Then just walk the word array using the numeric permutations as indices.
String[]words
)。Object[]
初始化的生成器,并且具有类似Object[] nextPermutation()
的方法。PS U 还可以使用整数排列生成器并生成从 0 到
(words.length - 1)
的所有排列;每个这样的排列都会为您提供一个要打印的words[]
索引数组。String[] words
).Object[]
, and has a method likeObject[] nextPermutation()
.PS U can also use a Integer permutation generator and generate all permutations from 0 to
(words.length - 1)
; each such permutation will give you an array of indexes ofwords[]
to be printed out.