http://poj.org/problem?id=1850
题意 :给定字符串,系统是用字符串组成的,字符串是按字典序排的。编码系统有三条规则,1这些的单词的长度是由小到大的,2相同长度的按字母在字典序的顺序排的,3 a-1,b-2,.....z-26,ab-27,..........,az-51,bc-52,............,vwxyz - 83681,.................。输入的是长度不超过10的全为小写英语字母的字符串,让你输出对应的数字,如果并没有按照顺序,则输出0 。
思路 :http://hi.baidu.com/lxyzmnwvhokptur/item/18c806469a668fe31e19bc1a#0这大神里边写的很详细,可以好好看看。
#include <iostream> #include <cstdio> #include <math.h> #include <string.h> using namespace std ; int com[31][31] = {0}; char ch[15] ; int main() { for(int i = 0 ; i <= 26 ; i++) for(int j = 0 ; j <= i ; j++) if(!j || i == j) com[i][j] = 1 ; else com[i][j] = com[i-1][j-1]+com[i-1][j] ; com[0][0] = 0 ; while(~scanf("%s",ch)) { for(int i = 0 ; i < strlen(ch)-1 ; i++) if(ch[i] > ch[i+1]) { printf("0 ") ; return 0 ; } int sum = 0 ; for(int i = 1 ; i < strlen(ch) ; i++) sum += com[26][i] ; for(int i = 0; i < strlen(ch) ; i++) { char sh = (!i)?'a':ch[i-1]+1 ; while(sh <= ch[i]-1) { sum += com['z'-sh][strlen(ch)-1-i] ; sh++ ; } } printf("%d ",sum+1) ; } return 0 ; }