[ACM]Alphacode

Time: 1sec Memory: 32MB

 

Problem

Alice and Bob need to send secret messages to each other and are discussing ways to encode their messages: Alice: "Let's just use a very simple code: We'll assign `A' the code word 1, `B' will be 2, and so on down to `Z' being assigned 26." Bob: "That's a stupid code, Alice. Suppose I send you the word `BEAN' encoded as 25114. You could decode that in many different ways!" Alice: "Sure you could, but what words would you get? Other than `BEAN', you'd get `BEAAD', `YAAD', `YAN', `YKD' and `BEKD'. I think you would be able to figure out the correct decoding. And why would you send me the word `BEAN' anyway?" Bob: "OK, maybe that's a bad example, but I bet you that if you got a string of length 500 there would be tons of different decodings and with that many you would find at least two different ones that would make sense." Alice: "How many different decodings?" Bob: "Jillions!" For some reason, Alice is still unconvinced by Bob's argument, so she requires a program that will determine how many decodings there can be for a given string using her code.

 

Input

Input will consist of multiple input sets. Each set will consist of a single line of digits representing a valid encryption (for example, no line will begin with a 0). There will be no spaces between the digits. An input line of `0' will terminate the input and should not be processed.

 

Output

For each input set, output the number of possible decodings for the input string. All answers will be within the range of a long variable.

 

Sample Input
25114
1111111111
3333333333
0
 
Sample Output
6
89
1

 

Alphacode
 1 #include <iostream>
 2 #include <string>
 3 #include <vector>
 4 using namespace std;
 5 
 6 vector <int> table; //用来存储计算结果的表
 7 
 8 int Calc(string &input, int pos)
 9 {
10     if(table[pos] != -1)
11     {   //如果已经求过这个值,则直接返回。
12         return table[pos];
13     }
14     if(input[pos] == '0')
15     {  //对串input当前扫描的pos位置为数字0时的处理
16         table[pos] = 0;
17         return 0;
18     }
19     else
20     {
21         if(pos >=input.size()-1 )
22         {  //递归终止条件
23             table[pos] = 1;
24             return 1;
25         }
26         else
27         {  //递归条用
28             int tmp = (input[pos] - '0'* 10 + (input[pos+1- '0');
29             if (tmp<=26)
30             {
31                 int res = Calc(input, pos+1+ Calc(input, pos+2);
32                 table [pos] = res;  //求得结果后填表,方便下次直接取答案
33                 return res;
34             }
35             else
36             {
37                 int res = Calc(input, pos+1);
38                 table [pos] = res; //求得结果后填表,方便下次直接取答案
39                 return res;
40             }
41         }
42     }
43 }
44 
45 int main()
46 {
47     string str;
48     while(cin>>str && str!="0")
49     {
50         table.assign(str.size()+1-1); 
51         cout<<Calc(str, 0)<<endl;
52     }
53     return 0;
54 }
55 

 

 

posted @ 2010-04-02 14:10  碧青_Kwok  阅读(428)  评论(0编辑  收藏  举报