1001. Alphacode

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值