用于创建所有可能的单词组合的 JAVA
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18271094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JAVA for creating all possible combination of words
提问by Vish
I am trying to get all the possible combinations of words input by user. Such as words input like aa, bb, cc
should give
我正在尝试获取用户输入的所有可能的单词组合。如单词输入喜欢aa, bb, cc
应该给
aa
bb
cc
aabb
aacc
bbaa
bbcc
ccaa
ccbb
aabbcc
aaccbb
bbaacc
bbccaa
ccbbaa
ccaabb
They can in any order but preferably in sorted order.
它们可以按任何顺序排列,但最好按排序顺序排列。
I have been trying to accomplish this for past hour and I think I am confused about something that I can't figure out and keep going in circles. Can someone please point me in the right direction.
在过去的一个小时里,我一直在努力实现这一点,我想我对一些我无法弄清楚的事情感到困惑并继续绕圈子。有人可以指出我正确的方向。
The code so far is
到目前为止的代码是
import java.util.Arrays;
import java.util.Scanner;
public class WordCombination {
private static String[] wordlist;
private static String[] wordcomb;
public static void main(String[] argv){
Scanner a = new Scanner(System.in);
System.out.print("Enter Words:");
setWords(a.nextLine());
creatCombinations();
}
private static void setWords(String words){
System.out.println("Entered words: " + words);
wordlist = words.split("\s+");
Arrays.sort(wordlist);
System.out.println(Arrays.toString(wordlist));
}
private static void creatCombinations(){
int size = wordlist.length;
int perm = (int) Math.pow(size, size);
int c = 1;
wordcomb = new String[perm];
String word = "";
/*
for(int i = 0; i < size; i++){
word = wordlist[i];
for(int j = 0; j < size; j++){
word += wordlist[j];
System.out.println(c + ": " + word + ".com");
c++;
}
word = "";
}*/
int l = 0;
for(int i = 0; i < size; i++){
int permj = (i+1) * size;
System.out.println("------------------> " + permj + " permutations for " + (i + 1) + " words combination");
int iterations = permj * (i+1);
System.out.println(" Iterations: " + iterations);
word = wordlist[i];
for(int j = 0; j < permj; j++){
for(int k = 0; k < i+1; k++){
int permi = i * size;
int index = permi + (k%permj);
wordcomb[c-1] += "" + wordlist[l%size];
out(l%size + "");
l++;
}
System.out.println(c + ": " + wordcomb[c-1]);
c++;
}
word = "";
}
}
private static void out(String s){
System.out.println(s);
}
}
}
*Edit: The output I am getting is *
*编辑:我得到的输出是 *
Enter Words:1 2 3
Entered words: 1 2 3
[1, 2, 3]
------------------> 3 permutations for 1 words combination
Iterations: 3
0
1: 1
1
2: 2
2
3: 3
------------------> 6 permutations for 2 words combination
Iterations: 12
Enter Words:aa bb cc
Entered words: aa bb cc
[aa, bb, cc]
------------------> 3 permutations for 1 words combination
Iterations: 3
0
1: aa
1
2: bb
2
3: cc
------------------> 6 permutations for 2 words combination
Iterations: 12
0
1
4: aabb
2
0
5: ccaa
1
2
6: bbcc
0
1
7: aabb
2
0
8: ccaa
1
2
9: bbcc
------------------> 9 permutations for 3 words combination
Iterations: 27
0
1
2
10: aabbcc
0
1
2
11: aabbcc
0
1
2
12: aabbcc
0
1
2
13: aabbcc
0
1
2
14: aabbcc
0
1
2
15: aabbcc
0
1
2
16: aabbcc
0
1
2
17: aabbcc
0
1
2
18: aabbcc
采纳答案by Bernhard Barker
I think your approach is fundamentally flawed. In your output, the first character of a permutation = the last character of the previous one + 1, and each character of a permutation = the previous character + 1, which is not how permutation work. So I don't think it can easily be fixed.
我认为你的方法从根本上是有缺陷的。在您的输出中,排列的第一个字符 = 前一个字符的最后一个字符 + 1,排列的每个字符 = 前一个字符 + 1,这不是排列的工作方式。所以我认为它不容易修复。
Below is a working way to do it, using recursion. It simply tries every character in a position, then recurses to the next position.
下面是一种使用递归的工作方法。它只是尝试一个位置中的每个字符,然后递归到下一个位置。
import java.util.Arrays;
public class NewMain
{
public static void main(String[] args)
{
c = Arrays.asList("aa","bb","cc").toArray(new String[0]);
permutation(0);
System.out.println("Number of permutations = " + count);
}
static String[] c;
static int count = 0;
static void swap(int pos1, int pos2)
{
String temp = c[pos1];
c[pos1] = c[pos2];
c[pos2] = temp;
}
public static void permutation(int start)
{
if (start != 0)
{
for (int i = 0; i < start; i++)
System.out.print(c[i]);
System.out.println();
count++;
}
for (int i = start; i < c.length; i++)
{
swap(start, i);
permutation(start + 1);
swap(start, i);
}
}
}
Prints:
印刷:
aa
aabb
aabbcc
aacc
aaccbb
bb
bbaa
bbaacc
bbcc
bbccaa
cc
ccbb
ccbbaa
ccaa
ccaabb
Number of permutations = 15
回答by Bernhard Barker
One problem is that wordcomb[c-1]
starts off as null
, not an empty string. When you add to it, null
gets converted to "null"
and the rest gets added to it.
一个问题是它wordcomb[c-1]
以 开头null
,而不是空字符串。当你添加到它时,null
被转换为"null"
,其余的被添加到它。
So you need to initialize wordcomb[c-1]
to an empty string:
所以你需要初始化wordcomb[c-1]
为一个空字符串:
for(int j = 0; j < permj; j++){
wordcomb[c-1] = ""; // <--------------------------
for(int k = 0; k < i+1; k++){
int permi = i * size;
int index = permi + (k%permj);
wordcomb[c-1] += "" + wordlist[l%size];
out(l%size + "");
l++;
}
System.out.println(c + ": " + wordcomb[c-1]);
c++;
}
回答by Frenk
This problem is basically caused by printing null String object. Try to check the creation or the printing. There is somewhere String that has null value :)
这个问题基本上是由打印空字符串对象引起的。尝试检查创建或打印。某处字符串具有空值:)
回答by Dmitry Nazarich
You should sort by invoking another method
您应该通过调用另一种方法进行排序
Arrays.sort(sortedIDXs, new Comparator<String>() {
public int compare(String idx1,String idx2) {
//Your method of sorting here
//return 1 if string idx1 should be before, 0 if equal and -1 in opposite
}