B - Code
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
#include<cstdio>
#include<stack>
#include<set>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstring>
#include<string>
#include<map>
#include<iostream>
#include<cmath>
using namespace std;
#define inf 0x3f3f3f3f
typedef long long ll;
const int N=225;
const int mmax = 3020+ 7;
int c[30][30];
char str[14];
void init()
{
memset(c,0,sizeof(c));
for(int i=0; i<=26; i++)
{
for(int j=0; j<=i; j++)
{
if(j==0||i==j)
c[i][j]=1;
else
c[i][j]=c[i-1][j-1]+c[i-1][j];
}
}
//从i个字母中取出j个字母,这j个字母的顺序是确定的
}
int main()
{
init();
while(scanf("%s",str)!=EOF)
{
int len=strlen(str);
int flag=0;
for(int i=0; i<len-1; i++)
{
if(str[i]>str[i+1]) //判断字母是否合理
{
printf("0\n");
flag=1;
break;
}
}
if(flag)
continue;
int sum=0;
for(int i=1; i<len; i++) //求比所给字符串短的情况,这些情况是排在str前面的
{
sum+=c[26][i];
}
char ch;
for(int i=0; i<len; i++) //求和str长度一样但排在str前面的
{
//先确定当前字符可以改变情况,第一个字符,则定义为‘a',非第一个字符,则一定要比前一个字符大
if(i==0)
ch='a';
else
ch=str[i-1]+1;
while(ch<=str[i]-1)//找到的字符应该比当前字符小
{
sum+=c['z'-ch][len-i-1]; /*找到当前字符更小的字符,那么该字符后面的
字符串应该从比替换后的当前字符大字符中找,找出来即是确定的顺序
*/
ch++;
}
}
printf("%d\n",sum+1); //前面有sum个序列,则是第sum+1个
}
return 0;
}