2
我需要生成一个字符串的所有可能的组合,使用多线程在N个线程之间平均分配工作。因此字符串cat
将输出:生成字符串与多线程的所有组合
c, a, t, ca, ct, ac, at, ta, tc, cat, cta, tac, tca, act, atc
每个线程包含startIndex
和endIndex
并执行字符串的不同处理。现在,我可以生成一个字符串的所有排列,但我完全无法理解我需要做什么来修改它以获取所有组合。任何帮助将不胜感激。
这就是我现在所拥有的:
public void run() {
for(int i = startIndex; (i < endIndex); i++) {
swap(word, 0, i); // Swap character to zero location.
permuteAndCheck(word, 1); // Recursively check permutations
swap(word, 0, i); // Undo swap
}
}
private void permuteAndCheck(char[] word, int start) {
if (start < word.length) {
// Not yet at the deepest recursion level to compare permutations.
for(int i = start; (i < word.length); i++) {
swap(word, start, i);
permuteAndCheck(word, start + 1);
swap(word, start, i); // Restore entry back from previous swap
}
return;
}
System.out.print(word + ", ");
}
private static final void swap(char[] word, int index1, int index2) {
char temp = word[index1];
word[index1] = word[index2];
word[index2] = temp;
}
不要忘记tca,atc和cta。 –
..除非命令没有关系,在这种情况下,你应该忘记那些。 但由于ac和ca都列出了... –
keshlam
我没有列出他们3。抱歉。 – arazzy