code
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 9282 | Accepted: 4432 |
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
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
char a[15];
int c[30][15];
//long long int c(int n,int m)
//{
// if(m>n-m)m=n-m;
// int i=n,j=m;
// double s=1;
// for( ; j>0; j--,i--)
// {
// s=s*(double)i/(double)j;
// }
// return (long long int )s;
//}
//long long int b[15];
int main()
{
c[0][0]=0;
for(int i=1; i<=26; i++)
for(int j=0; j<=i; j++)
{
if(!j||i==j)c[i][j]=1;
else c[i][j]=c[i-1][j-1]+c[i-1][j];<span style="font-family: Arial, Helvetica, sans-serif;"> //</span><span style="color:#ff0000;font-family: Arial, Helvetica, sans-serif;">初始化</span>
}
int sum;
while(~scanf("%s",a))
//scanf("%s",a);
{
int l=strlen(a);
int t=1;
for(int i=1; i<l; i++)
{
if(a[i]<=a[i-1])
{
t=0;
break;
}
}
if(!t)printf("0\n");
else
{
sum=0;
for(int i=1; i<l; i++)
sum+=c[26][i];
// printf("%d\n",sum);
for(int i=0; i<l; i++)
{
int s=a[i]-'a';
if(i==0) //找出在此字母之前的组合
{
if(s!=0)
{
for(int j=1; j<=s; j++)
{
sum+=c[26-j][l-1];
}
}
}
//else if(i==l-1)sum+=(s-(a[i-1]-'a'+1));
else
{
int ss=a[i-1]-'a';
for(int j=ss+1; j<s; j++)
{
sum+=c[26-j-1][l-i-1];
}
// sum++;
// printf("***sum=%d\n",sum);
}
// printf("%d\n",sum);
}
sum++;
printf("%d\n",sum);
}
}
return 0;
}