[ACM] poj 1496 Word Index(组合计数)
来源:互联网 发布:vs html5 js智能提示 编辑:程序博客网 时间:2024/05/22 13:45
Word Index
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 4541 Accepted: 2567
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.
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.
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
zacatvwxyz
Sample Output
261083681
Source
East Central North America 1995
题目中所输入的串必须严格上升是很关键的一点,在26个字母中随便选5个字母,则这5个字母只有一个排列(满足严格上升),所以根据此来计数。
比如要求 c e h 这个字典序是多少。该串长3 ,所以先把串长1和2的情况加上,即sum+=c (26,1) + c(26,2) , 该串为3了,注意到第一个字母最初应该从'a'开始,a->b 剩下两位随便在25->24个数里选 ,即 sum+=c (2 5,2) + c( 24 ,2) ,注意第一个字母不能到达 c,否则获得的串可能会比c e h大。给出的串第二个字母最初不能从a开始,因为要严格上升,要从原串上一个字母的下一个字母开始,第一个字母为c,所以第二个字母要从d开始计数, sum+=c( 24 , 1) ,最后一个字母也一样。这样最后所得的sum为 ceg的字典序,再加上1就是ceh的字典序了。 对于ceh 有 s+=c[26][1]+c[26][2]+c[25][2]+c[24][2]+c[22][1]+2+1;
代码:
#include <iostream>#include <string.h>using namespace std;int c[30][30];void C()//求组合数{ for(int i=0;i<=26;i++) { c[i][0]=c[i][i]=1; for(int j=1;j<i;j++) { c[i][j]=c[i-1][j]+c[i-1][j-1]; } }}int main(){ string s; C(); while(cin>>s) { int len=s.length(); int i; for(i=1;i<len;i++) { if(s[i]<=s[i-1]) break; } if(i<len) { cout<<0<<endl; continue; } int sum=0; for(i=1;i<len;i++) sum+=c[26][i];//长度比该串短的先加上 for(i=0;i<len;i++)//从高位进行处理对于每一位处理到该位的前一个,比如该位为‘d',就处理到c { char ch=i==0?'a':(s[i-1]+1); for(char j=ch;j<s[i];j++) sum+=c['z'-j][len-1-i]; } cout<<sum+1<<endl;//加上串本身 } return 0;}
1 0
- [ACM] poj 1496 Word Index(组合计数)
- poj 1496 Word Index 计数/组合数学
- POJ 1850 Code ,1496 Word Index(组合计数)
- Poj 1496 Word Index & Poj 1850 Code (计数 组合数)
- POJ 1496 Word Index组合数学(解题报告)
- POJ 1850 Code/POJ 1496 Word Index(组合数学-字母串序号)
- POJ 1850 Code && 1496 Word Index 组合数学
- POJ 1496 Word Index (组合数学 求字符串对应编码)
- POJ 1496 Word Index
- POJ 1496 Word Index
- POJ 题目1496 Word Index(排列组合)
- POJ 3761 (组合计数)
- POJ 1496 Word Index 笔记
- POJ 1496 POJ 1850 组合计数
- POJ 1496 Word Index || POJ 1850 Code
- POJ 1850 Code POJ 1496 Word Index
- POJ 3252 Round Numbers(组合计数)
- POJ 3252 组合计数
- 字符串匹配模版
- java 网络编程
- 多网卡的广播
- usaco-1.3.4Prime Cryptarithm
- 体验常成员函数
- [ACM] poj 1496 Word Index(组合计数)
- 编译原理(二)
- android应用开发——android存储之SharedPreferences
- Matlab中的函数
- Linux C之atoi()函数
- 赋值运算符函数
- THE CURE FOR THE COMMON THEME
- linux内核线程的创建与销毁
- 动态规划 传球问题