Description
Consider the English alphabet {a,b,c,…,z}. Using this alphabet, a set of valid words are to be formed that are in a strict lexicographic order. In this set of valid words, the successive letters of a word are in a strictly ascending order; that is, later letters in a valid word are always after previous letters with respect to their positions in the alphabet list {a,b,c,…,z}. For example,
abc aep gwz
are all valid three-letter words, whereas
aab are cat
are not.
For each valid word associate an integer which gives the position of the word in the alphabetized list of words. That is:
a -> 1
b -> 2
.
.
z -> 26
ab -> 27
ac -> 28
.
.
az -> 51
bc -> 52
.
.
vwxyz -> 83681
Your program is to read a series of input lines. Each input line will have a single word on it, that will be from one to five letters long. For each word read, if the word is invalid give the number 0. If the word read is valid, give the word’s position index in the above alphabetical list.
Input
The input will be terminated by end-of-file.
Output
Sample Input
z a cat vwxyz
Sample Output
26 1 0 83681
解题思路请在本站搜索poj 1850
#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;
while(scanf("%s",a)!=EOF)
{
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++)
{
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\n",sum);
}
}
return 0;
}
本文介绍了一种将特定类型的五字母或更少的英文单词编码为整数的简单编码方案。编码基于英文字母表,形成严格升序的词汇集合,并为每个有效单词分配其在字母排序列表中的位置。程序读取一系列输入单词,处理并输出相应的编码值或错误标识。
774

被折叠的 条评论
为什么被折叠?



