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