首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >查找包含重复字母的单词(排列)的排名

查找包含重复字母的单词(排列)的排名
EN

Stack Overflow用户
提问于 2014-03-26 01:33:14
回答 6查看 12.5K关注 0票数 7

虽然关于这个问题已经有很多帖子了,但我还是发了这篇文章。我不想把它作为答案发布,因为它不起作用。这篇文章的答案(Finding the rank of the Given string in list of all possible permutations with Duplicates)对我不起作用。

所以我尝试了这个(这是我抄袭的代码的汇编,我试图处理重复的代码)。不重复的情况运行良好。记账员生成83863,而不是所需的10743。

(阶乘函数和字母计数器数组'repeats‘工作正常。我没有发帖是为了节省空间。)

代码语言:javascript
复制
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;
    }
}
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22642151

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档