POJ

Transmitting and memorizing information is a task that requires different coding systems for the best use of the available space. A well known system is that one where a number is associated to a character sequence. It is considered that the words are made only of small characters of the English alphabet a,b,c, ..., z (26 characters). From all these words we consider only those whose letters are in lexigraphical order (each character is smaller than the next character). 

The coding system works like this: 
• The words are arranged in the increasing order of their length. 
• The words with the same length are arranged in lexicographical order (the order from the dictionary). 
• We codify these words by their numbering, starting with a, as follows: 
a - 1 
b - 2 
... 
z - 26 
ab - 27 
... 
az - 51 
bc - 52 
... 
vwxyz - 83681 
... 

Specify for a given word if it can be codified according to this coding system. For the affirmative case specify its code. 

Input

The only line contains a word. There are some constraints: 
• The word is maximum 10 letters length 
• The English alphabet has 26 characters. 

Output

The output will contain the code of the given word, or 0 if the word can not be codified.

Sample Input

bf

Sample Output

55

题意:求给出的字符串是排在第几位,而且必须是升序的

思路:选过的不能再选,我们要按照升序排列,我们就计算给定的字符串内有多少个字符串

假设给定长度为n的字符
我们就计算1,2,3....n-1的字符串出现的个数
例如:cfff
我就计算 长度为3的时候出现多少,因为长度为3的字符串肯定都出现过,都包括在给定字符串内,说明所有字母我们都可以使用
又要保持升序,说明字符不能重复,那我们就从26个字母内挑出3个字符进行升序排列 即 C263


由此我们可以得出长度为1,2的时候分别是C261  C262

然后第二部分就是求长度和当前长度一样的个数

我们遍历每个位置,当我们到达每个位置的时候我们前一个位置到下一个位置的活动范围有多少,因为必须是升序

我们就可以求当前还可以使用多少个字母,还有还有多少个位置没有被选,然后再次求得一个组合数

前提是必须比当前字母小1,因为只要小1,后面的字母才能是任意选,不然当前循环字母和原始字母一样,你无法确定后面的

字符串是否可以使用

#include<cstdio>
#include<cstring>
using namespace std;
int C[50][50];
int main()
{
    for(int i=0;i<=26;i++)
    {
        for(int j=0;j<=i;j++)
        {
            if(i==j||!j) C[i][j]=1;
            else C[i][j]=C[i-1][j-1]+C[i-1][j];
        }
    }
    char str[1001];
    long long sum=0;
    scanf("%s",str);
    int len=strlen(str);
    for(int i=1;i<len;i++)
    {
        if(str[i-1]<str[i]) continue;
        printf("0");
        return 0;
    }
    for(int i=1;i<=len-1;i++)
    {
        sum+=C[26][i];
    }
    for(int i=0;i<len;i++)
    {
        int t=i==0?'a':str[i-1]+1;
        while(t<str[i])
        {
            sum+=C['z'-t][len-i-1];
            t++;
        }
    }
    printf("%lld",sum+1);//最后再加上自己
}
原文地址:https://www.cnblogs.com/Lis-/p/9672356.html