Word Index
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
题目类型:组合数学
算法分析:求解在给定字符串之前有多少的字符串即可,详细内容可以参考:
http://blog.csdn.net/lyy289065406/article/details/6648492
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
#include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> #define lson rt << 1, l, m #define rson rt << 1 | 1, m + 1, r using namespace std; const int INF = 0x7FFFFFFF; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int MOD = 1000000000 + 7; const int maxn = 666; long long C[maxn][maxn]; void GetCom (long long val) { memset (C, 0, sizeof (C)); for (long long i = 0; i <= val; i++) { for (long long j = 0; j <= i; j++) { if (!j || i == j) C[i][j] = 1; else C[i][j] = C[i-1][j-1] + C[i-1][j]; } } C[0][0] = 0; } long long GetPerNumber (string s) { long long res = 0; //s的值,初始为0 /*计算长度比s小的字符串个数*/ for (long long i = 1; i < s.size (); i++) res += C[26][i]; //C[26][i]表示 长度为i的字符串的个数 /*计算长度等于len,但值比s小的字符串个数*/ for (long long i = 0; i < s.size (); i++) //i为s的指针,对每一个位置枚举 允许选择的字符ch { char ch = (!i) ? 'a' : s[i-1] + 1; //ch = s[i-1]+1 根据升序规则,当前位置的ch至少要比s前一位置的字符大1 while (ch <= s[i] - 1) //ch<=s[i]-1 根据升序规则,当前位置的ch最多只能比 s这个位置实际上的字符 小1 { res += C['z'-ch][s.size ()-1-i]; //'z'-ch : 小于等于ch的字符不允许再被选择,所以当前能够选择的字符总数为'z'-ch ch++; //len-1-i : ch位置后面(不包括ch)剩下的位数,就是从'z'-ch选择len-1-i个字符 } } return res + 1; // 此前的操作都是寻找比s小的所有字符串的个数,并不包括s本身,因此这里要+1 } int main() { // ifstream cin ("aaa.txt"); GetCom (26); string s; while (cin >> s) { long long i; for (i = 1; i < s.size (); i++) if (s[i-1] >= s[i]) break; if (i < s.size ()) cout << "0" << endl; else cout << GetPerNumber (s) << endl; } return 0; } |
- « 上一篇:poj1469
- poj1511:下一篇 »