Code POJ 1850 组合数学

来源:互联网 发布:rgb颜色渐变算法 编辑:程序博客网 时间:2024/05/03 16:08
Code
Time Limit: 1000MSMemory Limit: 30000KTotal Submissions: 7193Accepted: 3358

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

Source

 

 

 

这个题目实在是十分经典的,我想了很久...........

 

这个题目是说求给你的字符串的在字典中的位置,它的思路就是求出它的前面共有多少种情况就是了

举个例子来说比如说求abcd,那么我们先求出长度为1,长度为 2,长度为3的情况就是了,那么我们怎么长度为1时的组合是什么呢?当长度为 1 的时候,我们相当于从26个字母中挑选一个,那么排列数就是c[26][1],在对于三个数的时候,我们就相当于c[26][3],从26个字母中挑选三个数嘛,这时候有人会问会不会有排列的问题,不会有的,因为我们挑出的数只有从小到大一种排列,所以情况不过就是挑出的字母不同所以情况不同罢了。

 

那么长度等于本身的长度又该怎么办的????

如果我们要求出的数是bfh,那么是a--的肯定就是行的,这时候情况就是c[25][2],然后再挑出b--的情况,这时我们要挑的就是bc-,bd-,be-,分别是c[23][1],c[22][1],c[21][1],然后是bf_,只有bfg了,就是c[20][0],了,只剩一下一种了,就是这样了!!!!

 

#include<iostream>#include<cstdio>#include<cstring>using namespace std;int main(){    int i,j,k,len;    int c[30][30];    char s[20];    for(i=0;i<30;i++)    {        c[i][0]=1;        c[i][i]=1;    }    for(i=2;i<30;i++)    {        for(j=1;j<i;j++)        {        c[i][j]=c[i-1][j-1]+c[i-1][j];        }    }    int flag;    int sum;    while(scanf("%s",s)!=EOF)    {      len=strlen(s);      flag=1;      for(i=0;i<len-1;i++)      {          if(s[i+1]>s[i])          ;          else          {              flag=0;              break;          }      }       if(flag==0)       {          printf("0\n");       }       else       {        sum=0;        for(i=1;i<len;i++)        {            sum=sum+c[26][i];        }        char ch;        for(i=0;i<len;i++)        {            if(i==0)            ch='a';            else            ch=s[i-1]+1;            while(ch<=s[i]-1)            {                sum=sum+c['z'-ch][len-1-i];                ch++;            }        }        sum++;        printf("%d\n",sum);       }    }    return 0;}