poj1850 Code

POJ 1850 : Code
* 按照字典序的顺序从小写字母 a 开始按顺序给出序列 (序列中都为升序
字符串)
* a - 1
* b - 2
* ...
* z - 26
* ab - 27
* ...
* az - 51
* bc - 52
* ...
* vwxyz - 83681
* 输入字符串由小写字母 a-z 组成字符串为升序,根据字符串输出在字典
里的序列号为多少。

/*
组合数
题意是查一个字串的字典序排名
先求出长度比它小的,显然是ΣC 26 i(i<len)
然后求出长度等于它却比它小的。具体看代码。 
*/
#include<iostream>
#include<cstdio>
#include<cstring>

#define N 27

using namespace std;
int c[N][N],ans;
char str[N];

inline void combination()
{
    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()
{
    combination();
    while(cin>>str)
    {
        ans=0;
        int len=strlen(str);
        for(int i=1;i<len;i++)
          if(str[i]<=str[i-1])
            {
                cout<<"0"<<endl;
                return 0;
            }
        for(int i=1;i<len;i++) ans+=c[26][i];//长度小于它的所有方案 
        for(int i=0;i<len;i++)
        {
            char ch=(!i)?'a':str[i-1]+1;//比上一个大 
            while(ch<str[i])//比当前这个小 
            {
                ans+=c['z'-ch][len-i-1];//长度等于它且排在它前面的所有方案 
                ch++;
            }
        }
        cout<<++ans<<endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/thmyl/p/7425183.html