poj 1850(以字符a开始长度为len的个数+数字本身)

来源:互联网 发布:大淘客到底是什么软件 编辑:程序博客网 时间:2024/06/05 14:36
Code
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 7265 Accepted: 3398

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

Source

Romania OI 2002

一道按位dp题目。
关键状态是 dp[i][j] 表示 以i开头长度为j的字符串个数。
初始化为dp[i][1] = 1;
转移方程为 dp[i][j] = dp[i-1][j+1] + dp[i-1][j + 2]  + ... + dp[i-1][25];

再增加一个状态数组sum[i] 表示长度为i的满足条件的字符串有多少个,那么使用上面计算出来的dp可以很快求得。
那么以
vwxyz 为例:
首先result 先加上 sum[1] ~ sum[4]
然后对于长度为5的序列,需要一位一位讨论,这个和poj 3253按位计算的思路是一样的。
首先以小于v开头的长度为5的序列都满足要求。因此加上dp['a'][5] ~ dp['u'][5]
然后固定v,则以小于w大于v的序列也都满足要求。而这样的字母不存在。
然后固定前缀vw, 则以小于x大于w开头的序列也都满足要求。这个数字也是0.
以此类推,固定前缀vwx, vwxy, vwxyz求得的数字也都是0。。

最后再加上那个数字本身(再加一)

提交记录:
1、WA 因为没有看到题目中的要求,当字符串不合法时,输出0.
2、Accepted