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
代码实现:
//用1-26分别标记a-z,计算一串数字所表示的所有解码可能数,如:25114有6种可能
//涉及到递归的思想
//动态规划思想(重要)
//by--catherine--on 2016,04,05
/*
Sample Input
25114
1111111111
3333333333
0
Sample Output
6
89
1
*/
#include<iostream>
#include<string>
#include<iomanip>
#include<memory.h>
using namespace std;
int main() {
string s;
while (cin >> s && s != "0") {
int l = s.length();
long long totalNumber[l];
memset(totalNumber, 0, sizeof(totalNumber));
totalNumber[0] = 1;
for (int i = 1; i < l; i++) {
if (s[i] == '0') {
if (i == 1) totalNumber[i] = totalNumber[i - 1];
else totalNumber[i] = totalNumber[i - 2]; //为了后面的递归
} else {
if ((s[i - 1] == '1'&&s[i] <= '9')||(s[i - 1] == '2'&&s[i] <= '6')) {
//注意不能将‘2’写为2
if (i == 1) totalNumber[i] = totalNumber[i - 1] + 1;
else totalNumber[i] = totalNumber[i - 1] + totalNumber[i - 2];
//增加一个数后,分两种可能,即给数与前一个数能够组合为二位数,此时有totalNumber
//[i - 2]
//种可能,若不能与前一个数组合为二位数,则有totalNumber[i - 1]种可能。
//次数用到了递归的思想还有数学的组合数
} else {
totalNumber[i] = totalNumber[i - 1];
}
}
}
cout /*<< "The total number of the posibility is : " */<< totalNumber[l - 1]
<< endl;
}
return 0;
}