# POJ 1850 Code (组合数学)

Code

 Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 9588 Accepted: 4584

Description

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

### 思路

​ 以a开头的，有ab,ac,…az，一共 C=25 个。

​ 以b开头的，有bc,bd,…bz，一共 C=24 个。

​ ……

​ 以x开头的，有xy,xz，一共 C=2 个。

​ 以y开头的，有yz，一共 C=1 个。

C[n][m]=∑{i=1->n−1}C[i][m-1]

### AC 代码

#include <iostream>
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<cmath>
#include<stack>
#include<string.h>
#define PI (acos(-1))
using namespace std;
int C;
void init() //组合数打表
{
C=C=1;
for(int i=2; i<27; i++)
for(int j=0; j<=i; j++)
{
if(j==0)C[i][j]=1;
else C[i][j]=C[i-1][j-1]+C[i-1][j];
}
}
int main()
{
init();
string c;
while(cin>>c)
{
int len=c.length(),sum=0;
for(int i=1; i<len; i++)
if(c[i]<=c[i-1])
{
printf("0\n");
return 0;
}

for(int i=1; i<len; i++)    //长度小于len的字符串
sum+=C[i];

for(int i=0; i<len; i++)    //长度等于len的字符串
{
char ch=i!=0?c[i-1]+1:'a';  //对于第一位，起始最小为'a'，其余为当前已有位+1
while(ch<c[i])
sum+=C['z'-ch++][len-1-i];
}
printf("%d\n",sum+1);
}
return 0;
}