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
bfSample Output
55 题目链接:http://poj.org/problem?id=1850
并不晓得为什么这道题会归到组合数学中去,可能是因为组合数吧,从网上跟大神学了一招,可以用杨辉三角来预处理组合数,当然,你也可以一个一个的计算。
这个题的数学的符号不好打,我从网上找了个大神的博客,他讲的很清楚,最重要的就是两个式子,组合数的转换,大神的博客上有,我打不出来。。。。。尴尬中。。。。。
大神的博客、
http://blog.csdn.net/lyy289065406/article/details/6648492
代码:
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; int a[50][50]; void yanghui()//杨辉三角预处理组合数 { int i,j; for(i=0;i<=26;i++) { for(j=0;j<=i;j++) { if(!j||j==i) a[i][j]=1; else a[i][j]=a[i-1][j]+a[i-1][j-1]; } } a[0][0]=0; return ; } int main() { char s[100]; yanghui(); int sum=0; int i; while(~scanf("%s",s)) { int len=strlen(s); for(i=1;i<len;i++)//不符合条件 { if(s[i-1]>=s[i]) { break; } } if(i!=len) { cout<<"0"<<endl; continue; } for(i=1;i<len;i++)//计算比s字符串长度小的值的个数 { sum+=a[26][i];//a[26][i]表示 长度为i的字符串的个数 } char ch; for(i=0;i<len;i++) { ch=(i==0)?'a':s[i-1]+1;// 根据升序规则,当前位置的ch至少要比s前一位置的字符大1 while(ch<=s[i]-1)//根据升序规则,当前位置的ch最多只能比 s这个位置实际上的字符 小1 { sum+=a[(int)('z'-ch)][len-i-1];// 小于等于ch的字符不允许再被选择,所以当前能够选择的字符总数为'z'-ch ch++;// ch位置后面(不包括ch)剩下的位数,就是从'z'-ch选择len-1-i个字符 } } printf("%d\n",sum+1);//别忘了sum要+1; } return 0; }
