poj1850-CODE-组合

求出给定序列的序号。有一个定理需要知道

具体看这篇博客吧http://blog.csdn.net/lyy289065406/article/details/6648492

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

char str[20];
int ans=0,C[26][26];

void init(void)
{
    for(int i=0;i<=26;i++)
        for(int j=0;j<=i;j++)
            if(!j || i==j)
                C[i][j]=1;
            else
                C[i][j]=C[i-1][j-1]+C[i-1][j];
    C[0][0]=0;
    return;
}


int main()
{
    init();
    while(~scanf("%s",str))
    {
        int len = strlen(str);
        char oc = str[0],nc = str[0];
        int ok = 1;
        ans=0;
        for(int i=1;i<len;i++)
        {
            nc = str[i];
            if(nc <= oc)
            {
                printf("0
");
                ok = 0;
                break;
            }
        }
        if(!ok) continue;

        for(int i=1;i<len;i++)
            ans += C[26][i];

        for(int i=0;i<len;i++)
        {
            char ch = i?str[i-1]+1:'a';
            while(ch < str[i])
            {
                ans += C['z'-ch][len-1-i];
                ch++;
            }
        }
        printf("%d
",ans+1);
    }
}
原文地址:https://www.cnblogs.com/helica/p/5168392.html