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
55Source
Romania OI 2002也是找规律嘛
算出它之前的字符有多少个,再加上一
先把长度小于的算出来,C26,k ,k是字符串的长度
然后把每位字符类似成数位
从最高位向下找,找比这位小的字符串总数(不包括等于)
例如求ceg
先找长度为1,2的字符串个数
在从最高位第一个c字符开始,找最高位为a,b的字符个数(a:C26-(‘a'-’a‘+1) , len-i ; b : C26-(‘b'-’a‘+1),len-i ,其中i代表第几位)
第二位就找比e小的,注意 你已经找了第一位是a或b,假如第二位是a,b,c,第一位肯定是a或b,所以第二位要从d开始,依次向下类推,最后前面的字符串个数加一就是要求的编号
#include<stdio.h> #include<string.h> #include<math.h> #include<string> #include<algorithm> #include<queue> #include<vector> #include<map> #include<set> typedef long long ll; using namespace std; int myc(int n,int r) { int sum=1; for(int i=1;i<=r;i++) { sum=sum*(n+1-i)/i; } return sum; } int mya(int n,int r) { int sum=1; for(int i=0;i<r;i++) sum=sum*(n-i); return sum; } int ans(char ch[],int len) { int ans=0,i,j,t; for(i=1;i<len;i++) { ans+=myc(26,i); }// t=0; // printf("ha%d\n",ans); for(i=0;i<len;i++) { for(j=t;j<ch[i]-'a';j++) { ans+=myc(26-j-1,len-i-1); } t=ch[i]-'a'+1; } return ans; } int main() { char ch[20],maxx; int len,i; scanf("%s",ch); len=strlen(ch); maxx=ch[0]; for(i=1;i<len;i++) { if(ch[i]<=maxx) { printf("0\n"); return 0; } else maxx=ch[i]; } printf("%d\n",ans(ch,len)+1); return 0; }