标签:
题目连接:http://soj.sysu.edu.cn/1001
解题报告:f[i]表示第i个字母前能组成的种类
情况:1、s[i]==0,f[i]=f[i-1]
2、s[i-1]*10+s[i]>=10&&<=26,f[i]=f[i-1]+f[i-2]
3、!s[i-1]*10+s[i]>=10&&<=26,f[i]=f[i-1];
#include <iostream> #include <cstring> #include <string> using namespace std; int f[10000] ; int main() { string s ; while(cin >> s && s[0] != ‘0‘) { s = ‘ ‘ + s ; memset(f,0,sizeof(f)) ; f[0] = 1 ; f[1] = 1 ; for(int i = 2 ; i < s.size() ;i ++) { if(s[i] == ‘0‘) { f[i] = f[i-2] ; } else { f[i] = f[i-1]; if(10*(s[i-1] - ‘0‘)+s[i]-‘0‘ > 9 && 10*(s[i-1] - ‘0‘)+s[i]-‘0‘ <= 26) { f[i] = f[i] + f[i-2] ; } } } cout << f[s.size()-1] << endl; } }
标签:
原文地址:http://www.cnblogs.com/biong-blog/p/4493816.html