POJ 题目1496 Word Index(排列组合)
2015-01-11 18:33
411 查看
Word Index
Description
Encoding schemes are often used in situations requiring encryption or information storage/transmission economy. Here, we develop a simple encoding scheme that encodes particular types of words with five or fewer (lower case) letters as integers.
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:
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 consists of a series of single words, one per line. The words are at least one letter long and no more that five letters. Only the lower case alphabetic {a,b,...,z} characters will be used as input. The first letter of a word will appear as the first
character on an input line.
The input will be terminated by end-of-file.
Output
The output is a single integer, greater than or equal to zero (0) and less than or equal 83681. The first digit of an output value should be the first character on a line. There is one line of output for each input line.
Sample Input
Sample Output
Source
East Central North America 1995
ac代码
#include<stdio.h>
#include<string.h>
#include<math.h>
int c[30][30];
void fun()
{
int i,j;
for(i=0;i<30;i++)
{
c[i][0]=1;
}
for(i=1;i<30;i++)
{
for(j=1;j<30;j++)
c[i][j]=c[i-1][j-1]+c[i-1][j];
}
}
int main()
{
char s[20];
fun();
while(scanf("%s",s)!=EOF)
{
int len=strlen(s);
__int64 sum=0;
int i,j,k;
for(i=0;i<len-1;i++)
{
if(s[i]>s[i+1])
{
printf("0\n");
// return 0;
break;
}
}
if(i<len-1)
continue;
for(i=1;i<len;i++)
{
sum+=c[26][i];
}
for(i=0;i<len;i++)
{
int ch;
if(i==0)
ch='a';
else
ch=s[i-1]+1;
while(ch<s[i])
{
sum+=c['z'-ch][len-i-1];
ch++;
}
}
printf("%I64d\n",++sum);
}
}
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 4700 | Accepted: 2666 |
Encoding schemes are often used in situations requiring encryption or information storage/transmission economy. Here, we develop a simple encoding scheme that encodes particular types of words with five or fewer (lower case) letters as integers.
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 consists of a series of single words, one per line. The words are at least one letter long and no more that five letters. Only the lower case alphabetic {a,b,...,z} characters will be used as input. The first letter of a word will appear as the first
character on an input line.
The input will be terminated by end-of-file.
Output
The output is a single integer, greater than or equal to zero (0) and less than or equal 83681. The first digit of an output value should be the first character on a line. There is one line of output for each input line.
Sample Input
z a cat vwxyz
Sample Output
26 1 0 83681
Source
East Central North America 1995
ac代码
#include<stdio.h>
#include<string.h>
#include<math.h>
int c[30][30];
void fun()
{
int i,j;
for(i=0;i<30;i++)
{
c[i][0]=1;
}
for(i=1;i<30;i++)
{
for(j=1;j<30;j++)
c[i][j]=c[i-1][j-1]+c[i-1][j];
}
}
int main()
{
char s[20];
fun();
while(scanf("%s",s)!=EOF)
{
int len=strlen(s);
__int64 sum=0;
int i,j,k;
for(i=0;i<len-1;i++)
{
if(s[i]>s[i+1])
{
printf("0\n");
// return 0;
break;
}
}
if(i<len-1)
continue;
for(i=1;i<len;i++)
{
sum+=c[26][i];
}
for(i=0;i<len;i++)
{
int ch;
if(i==0)
ch='a';
else
ch=s[i-1]+1;
while(ch<s[i])
{
sum+=c['z'-ch][len-i-1];
ch++;
}
}
printf("%I64d\n",++sum);
}
}
相关文章推荐
- poj 1496 Word Index
- [ACM] poj 1496 Word Index(组合计数)
- [ACM] poj 1496 Word Index(组合计数)
- POJ 1496 Word Index
- POJ1496-Word Index
- POJ 1496 Word Index 笔记
- [ACM] poj 1496 Word Index(组合计数)
- POJ 1850 Code POJ 1496 Word Index
- POJ1496-Word Index
- Poj 1496 Word Index & Poj 1850 Code (计数 组合数)
- POJ 1850 Code POJ 1496 Word Index
- 排列组合:poj 1496 Word Index+poj 1850 Code
- poj 1850 Code(同 poj 1496 Word Index)
- POJ 1496 Word Index || POJ 1850 Code
- POJ 1496 Word Index 和POJ 1850 Code 动态规划
- poj1496—Word Index(组合数)
- POJ 1496 Word Index 和POJ 1850 Code 动态规划
- POJ 1496 Word Index (组合数学 求字符串对应编码)
- POJ 1496 Word Index
- (Relax 数论1.26)POJ 1496 Word Index(计算一个字符串在字典中的位置)