poj1496—Word Index(组合数)
来源:互联网 发布:民国算法 编辑:程序博客网 时间:2024/06/07 21:55
题目链接:传送门
Word Index
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 5120 Accepted: 2896
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
解题思路:对于一个字符串,用组合数统计他前面合法串的个数n,编号就是n+1
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <string>#include <stack>#include <queue>#include <map>using namespace std;typedef long long ll;const int N = 501000;const int M = 300100;const int INF = 0x3fffffff;const double eps = 1e-8;const double PI = acos(-1.0);const int mod = 1e9+7;typedef pair<int,int>PA;int arr[5];int comb(int n,int m){ if(n == m || m == 0) return 1; else return comb(n-1,m)+comb(n-1,m-1);}//利用组合数,对于一个字符串,统计他前面合法串的个数n,该串的编号就是n+1string st;int solve(){ for( int i = 1 ; i < st.length() ; ++i ){ if( st[i] <= st[i-1] ) return 0; } int ans = 0; int len = st.length(); ans = arr[len-1]; int num,pos = 0; for( int i = 0 ; i < len ; ++i ){ num = st[i]-'a'-pos; for( int j = 0 ; j < num ; ++j ){ pos++; ans += comb(26-pos,len-i-1); } pos++; } return ans+1;}int main(){ arr[0] = 0; for( int i = 1 ; i <= 4 ; ++i ) arr[i] = comb(26,i); for( int i = 1 ; i <= 4 ; ++i ) arr[i] += arr[i-1]; while( cin >> st ){ cout << solve() << endl; } return 0;}
阅读全文
0 0
- poj1496—Word Index(组合数)
- poj1850/poj1496 组合数
- POJ1496 Word Index【全排列】
- POJ1469 Word Index (组合数)
- Poj 1496 Word Index & Poj 1850 Code (计数 组合数)
- Uva 417 - Word Index 解题报告(组合数)
- poj1496
- poj1496(组合数学)-与1850几乎一样
- poj 1496 Word Index 计数/组合数学
- POJ 1850 Code && 1496 Word Index 组合数学
- POJ 1850 Code ,1496 Word Index(组合计数)
- POJ 1496 Word Index组合数学(解题报告)
- [ACM] poj 1496 Word Index(组合计数)
- POJ 1496 Word Index (组合数学 求字符串对应编码)
- Word Index
- Word Index
- POJ1850&&POJ1496
- POJ 1850 Code/POJ 1496 Word Index(组合数学-字母串序号)
- How Many Tables HDU
- Spring 事物手动回滚
- 表单验证 datetype 使用
- Python——列表生成式备忘
- Codeforces785D (组合数的逆元)
- poj1496—Word Index(组合数)
- c#基础 (一)数据类型,数据类型转换 结构体,构造函数
- PHP基础教程-11 创建单位转换器
- Android开发之sqlite数据库的应用
- android学习——使用TableLayout动态生成表格,并为tablerow中的列添加点击监听
- nlp中加入attention
- [PAT乙级]1048. 数字加密(20)
- 剑指Offer读书笔记5
- hdu2767(强连通分量+缩点)