poj1850 Code

来源:互联网 发布:php jenkins docker 编辑:程序博客网 时间:2024/06/17 17:31
Code
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 7638 Accepted: 3591

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 the code of the given word, or 0 if the word can not be codified.

Sample Input

bf

Sample Output

55


大致题意: 给定一个字符串,问这个字符串按照给定规律的位置。

         规律:字符串<=10,(s[i-1] < s[i]),如果存在s[i-1] >= s[i],则输出0

解题思路:找出规律就容易解答了,当有一个字符时, ans = 26;

         当有两个字符时a(b-z) = 25, b(c-z) = 24, c(d-z) = 23, ... ,yz = 1.

                      = c(25,1) + c(24,1) + ... + c(n, 1) + c(n-1,1) + c(1,1)

                    因为  c(n,m) = c(n-1,m-1) +c(n-1,m);

                         c(n-1,m) = c(n-2, m-1) + c(n-2, m);

                         c(n-2,m) = c(n-3, m-1) + c(n-3, m);

                         ...

                         c(m+1, m) = c(m, m-1) + c(m, m) = c(m, 1) + 1;

                         c(m, m) = 1;

                    所以  c(n, m) = c(n-1, m-1) + c(n-2, m-1) + ... + c(m, m-1) + 1;

             c(n, m) =c(n-1, m-1) + c(n-2, m-1) + ... + c(2, m-1) + c(1, m-1);

              (c(s,t) = 0 t>s);

           所以两个字符为c(26,2), 同理 3个 c(26,3) ... 10个 c(26, 10);

例如给bfgi  = 1.[len<4的所有] + 2.[len = 4(小于 bfgi)];

           1. 将所有len<4的c(26,i) 和计算;

           2. 就要枚举<bfgi(长度相等的字符串), 如:a..., (ba.. to be..), (bfg. to bfgh)

           也就是为a时 c('z' - 'a', len-pos(a)-1), 为什么是('z' - 'a'),

           因为a下一个,从b-z,len-pos(a)-1就是除了a余下的长度,同理可得余下的。


代码:

#include <stdio.h>#include <string.h>int sum;int c[27][27] = {0};char str[20];void init(){int i, j;for (i=0; i<27; ++i){for (j=0; j<=26; ++j){if (!j || i==j)c[i][j] = 1;elsec[i][j] = c[i-1][j-1] + c[i-1][j];}}}void solve(){int len = strlen(str);sum = 0;int i, j;for (i=1; i<len; ++i)sum += c[26][i];char ch;for (i=1; i<len; ++i){if (str[i-1] >= str[i]){puts("0");return;}}for (i=0; i<len; ++i){ch = i==0?'a':str[i-1]+1;while (ch < str[i]){sum += c['z' - ch][len-i-1];ch++;}}sum++;printf("%d\n", sum);}int main(){init();while (~scanf("%s", str)){solve();}return 0;}


0 0