算法想到了,题目坑太多,数组,含‘0’
#include <cstdio> #include <algorithm> #include <cstring> #include <iostream> #define LL long long const int MAXN = 50100; using namespace std; char str[MAXN]; LL dp[MAXN]; int main() { while(gets(str+1)) { int len = strlen(str+1); if(str[1] == ‘0‘ && len == 1) { break; } dp[0] = dp[1] = 1; for(int i = 2; i <= len; ++i) { if(str[i] != ‘0‘) { dp[i] = dp[i-1]; int num = (str[i-1] -‘0‘) * 10 + str[i] - ‘0‘; if(num >= 11 && num <= 26) dp[i] += dp[i-2]; } else { if(str[i-1] == ‘0‘) dp[i] = dp[i-1]; else if(str[i-1] == ‘1‘ || str[i-1] == ‘2‘) dp[i] = dp[i-2]; } } cout << dp[len] << endl; } return 0; }
时间: 2024-11-08 09:21:25