Description
Transmitting and memorizing information is a task that requires different coding systems for the best use of the available space. A well known system is that one where a number is associated to a character sequence. it is considered that the words are made only of small characters of the English alphabet a, B, c ,..., z (26 characters ). from all these words we consider only those whose letters are in lexigraphical order (each character is smaller than the next character ).
The coding system works like this:
? The words are arranged in the increasing order of their length.
? The words with the same length are arranged in lexicographical order (the order from the dictionary ).
? We codify these words by their numbering, starting with a, as follows:
A-1
B-2
...
Z-26
AB-27
...
Az-51
Bc-52
...
Vwxyz-83681
...
Specify for a given word if it can be codified according to this coding system. For the affirmative case specify its code.
Input
The only line contains a word. There are some constraints:
? The word is maximum 10 letters length
? The English alphabet has 26 characters.
Output
The output will contain in the code of the given word, or 0 if the word can not be codified.
Sample Input
Bf
Sample Output
55
Definition:
A-> 1, B-> 2 ...... Z-> 26, AB-> 27 ...... Vwxyz-> 83681.
The valid string sequence is that each lowercase letter is greater than the ASCII code of the last lowercase letter, and the invalid output is 0.
Idea: If the length is smaller than len, it is to calculate C [26] [{1, 2... len-1}], as for the case equal to len is to discuss str [i-1] + 1 to str [I]
# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <cmath> using namespace std; const int maxn = 50; char str [maxn]; int C [maxn] [maxn]; void init () {C [0] [0] = 1; C [1] [0] = C [1] [1] = 1; for (int I = 2; I <27; I ++) {C [I] [I] = C [I] [0] = 1; for (int j = 1; j <I; j ++) C [I] [j] = C [i-1] [j] + C [i-1] [j-1];} int main () {init (); while (scanf ("% s", str )! = EOF) {int flag = 1; int len = strlen (str); for (int I = 1; I <len; I ++) if (str [I] <= str [i-1]) {flag = 0; break;} if (! Flag) {printf ("0 \ n"); continue;} int ans = 0; for (int I = len-1; I> 0; I --) ans + = C [26] [I]; for (int I = 0; I <len; I ++) {char ch = (I = 0 )? 'A': (str [i-1] + 1); for (int j = ch; j <str [I]; j ++) ans + = C ['Z'-j] [len-1-i];} printf ("% d \ n", ans + 1);} return 0 ;}
POJ-1850 Code