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\n") ;
            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\n",sum+1) ;
    }
    return 0 ;
}
View Code

 

posted on 2013-11-22 18:33  枫、  阅读(260)  评论(0编辑  收藏  举报