您的位置:首页 > 其它

poj1496—Word Index(组合数)

2017-08-04 20:17 489 查看
题目链接:传送门

Word Index

Time Limit: 1000MS Memory Limit: 10000K
Total 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: 
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


解题思路:对于一个字符串,用组合数统计他前面合法串的个数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+1

string 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: