Description
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 word is maximum 10 letters length
• The English alphabet has 26 characters.
Output
Sample Input
bf
Sample Output
55
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
__int64 factorial(int n,int m)
{
__int64 sum=1;
int i;
for(i=n;i>n-m;i--)
{
sum*=i;
}
return sum;
}
__int64 factorial2(int n)
{
__int64 sum=1;
int i;
for(i=n;i>=2;i--)
{
sum*=i;
}
return sum;
}
__int64 factorial3(int n,int i)
{
return factorial(n,i)/factorial2(i);
}
int main()
{
char a[100];
int str,i,j;
__int64 sum;
//cout<<factorial(26,1)/factorial2(1)<<endl;
scanf("%s",a);
str=strlen(a);
sum=0;
int flag=0;
for(i=1;i<str;i++)//判断是不是升序
{
if(a[i]<a[i-1])
{
flag=1;
break;
}
}
if(flag)
{
printf("0\n");
}
else
{
for(i=1;i<=str;i++)//求出到str这么多字符的所有数量
{
sum+=factorial3(26,i);
}
//printf("%I64d\n",sum);
for(i=1,j=str-1;j>=0;j--)//减去多了的
{
sum-=factorial3('z'-a[j],i);
i++;
}
printf("%I64d",sum);
}
return 0;
}