first blood, personal blog!动态规划,dp[i]表示前i个数字能够解码出来的字符串数,初始化dp[0] = dp[1] = 1。将数字串扫一遍,看每一个数字是否能与前一个数字构成一个有效的两位数,注意0的情况。
// Problem#: 1001
// Submission#: 2184264
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <iostream>
#include <cstdio>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
vector<int> dp(1000005);
int main()
{
string str;
while(cin >> str && str != "0"){
dp.clear();
dp[0] = dp[1] = 1;
int tmp1, tmp2;
int len = str.size();
for(int i = 1; i < len; i++){
tmp1 = str[i]-'0';
tmp2 = str[i-1]-'0';
if(tmp1 == 0){
if(tmp2 == 1 || tmp2 == 2)
dp[i+1] = dp[i-1];
else{
dp[len] = 0;
break;
}
}
else if(tmp2 == 1){
dp[i+1] = dp[i] + dp[i-1];
}
else if(tmp2 == 2){
if(tmp1 >= 1 && tmp1 <= 6)
dp[i+1] = dp[i] + dp[i-1];
}
else
dp[i+1] = dp[i];
//cout<<dp[i+1]<<endl;
}
cout << dp[len] <<endl;
}
}