Code
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 9236 | Accepted: 4405 |
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.
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.
• 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
大致题意:(与POJ1496基本一致)
输出某个str字符串在字典中的位置,由于字典是从a=1开始的,因此str的位置值就是 在str前面所有字符串的个数 +1
规定输入的字符串必须是升序排列。不降序列是非法字符串
不要求用循环输入去输入若干组字符串,但若输入非法字符串则输出0,且结束程序,这是和POJ1496最猥琐的区别,很多同学只注意到规定str的长度不同,以为把str数组长度改一下直接复制就能AC再多刷一题了,殊不知老是WA却找不到原因,大概就是这里出问题了.本题Str最长为10个字符
#include<cstdio> #include<cstring> using namespace std; #define N 1010 char str[N]; int fac(int x){ int s=1; for(int i=2;i<=x;i++){ s*=i; } return s; } int C(int n,int m){//如果用常规的 以前的很笨的方法,会造成大数的溢出 return fac(n)/fac(m)*fac(n-m); } int com(int n,int k){ if(k==0) return 1; return com(n-1,k-1)*n/k; } int main(){ scanf("%s",str); int len=strlen(str); int sum=0; for(int i=0;i<len-1;i++){//把前面的都加起来,看看这个字符串len-1个长度的+len-2……1的字符串一共有多少个 sum+=com(26,i+1); } for(int i=0;i<len;i++){ for(int j=i+1;j<len;j++){ if(str[i]>str[j]){//如果字符串不是按照升序排列的,那么要输出0; puts("0");return 0; } } } for(int i=0;i<len;i++){//看看当前的字符串在“本长度”中排在什么样的位置。 for(int j=(i==0?0:str[i-1]-'a'+1);j<str[i]-'a';j++){//现在依然是在计算 与当前字符串一样长度的字符串的数量(之前的 ~ ) sum+=com(26-j-1,len-i-1);//这个组合是固定开头的字符,选取后面的! }//计算的过程是比如说第一个位置是k,开始固定'a'那么以后的len-1个位置上就是从b~z中选出组合,然后再固定b再选,…… }//直到第一个位置是k本身,第一个位置枚举完了,枚举地二个位置,从a开始直到第二个位置的字符本身,依次类推,直到最后一个字符枚举完毕;then本字符串之前的就计算完了,因为这样枚举都是本字符串之前的 printf("%d ",sum+1); return 0; }
那就再刷一遍POJ1496
#include<iostream> #include<cstdio> #include<cstring> using namespace std; #define N 1010 char str[N]; int fac(int x){ int s=1; for(int i=2;i<=x;i++){ s*=i; } return s; } int C(int n,int m){ return fac(n)/fac(m)*fac(n-m); } int com(int n,int k){ if(k==0) return 1; return com(n-1,k-1)*n/k; } int main(){ while(cin>>str){ int len=strlen(str); int sum=0,flag=0; for(int i=0;i<len-1;i++){ sum+=com(26,i+1); } for(int i=0;i<len;i++){ for(int j=i+1;j<len;j++){ if(str[i]>=str[j]){ puts("0");flag=1;i=len;break; } } } if(flag) continue; for(int i=0;i<len;i++){ for(int j=(i==0?0:str[i-1]-'a'+1);j<str[i]-'a';j++){ sum+=com(26-j-1,len-i-1); } } cout<<sum+1<<endl; } return 0; }