【发布时间】:2014-05-03 17:49:38
【问题描述】:
尽管已经发布了很多关于这个问题的帖子,但我还是发布了这个。我不想发布作为答案,因为它不起作用。这篇文章的答案 (Finding the rank of the Given string in list of all possible permutations with Duplicates) 对我不起作用。
所以我尝试了这个(这是我抄袭的代码的汇编以及我处理重复的尝试)。非重复案例工作正常。 BOOKKEEPER 生成 83863,而不是所需的 10743。
(阶乘函数和字母计数器数组“重复”工作正常。我没有发布以节省空间。)
while (pointer != length)
{
if (sortedWordChars[pointer] != wordArray[pointer])
{
// Swap the current character with the one after that
char temp = sortedWordChars[pointer];
sortedWordChars[pointer] = sortedWordChars[next];
sortedWordChars[next] = temp;
next++;
//For each position check how many characters left have duplicates,
//and use the logic that if you need to permute n things and if 'a' things
//are similar the number of permutations is n!/a!
int ct = repeats[(sortedWordChars[pointer]-64)];
// Increment the rank
if (ct>1) { //repeats?
System.out.println("repeating " + (sortedWordChars[pointer]-64));
//In case of repetition of any character use: (n-1)!/(times)!
//e.g. if there is 1 character which is repeating twice,
//x* (n-1)!/2!
int dividend = getFactorialIter(length - pointer - 1);
int divisor = getFactorialIter(ct);
int quo = dividend/divisor;
rank += quo;
} else {
rank += getFactorialIter(length - pointer - 1);
}
} else
{
pointer++;
next = pointer + 1;
}
}
【问题讨论】:
-
我想你想要词典排序?
-
是的,大卫 - 例如QUESTION=24572(在我的代码中工作,因为没有欺骗。)感谢您的回复。
标签: string algorithm permutation