POJ2033 LA3078 HDU1508 ZOJ2202 AlphacodeDFS+DP
Posted 海岛Blog
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ2033 LA3078 HDU1508 ZOJ2202 AlphacodeDFS+DP相关的知识,希望对你有一定的参考价值。
Alphacode
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 15489 Accepted: 4649
Description
Alice and Bob need to send secret messages to each other and are discussing ways to encode their messages:
Alice: “Let’s just use a very simple code: We’ll assign ‘A’ the code word 1, ‘B’ will be 2, and so on down to ‘Z’ being assigned 26.”
Bob: "That’s a stupid code, Alice. Suppose I send you the word ‘BEAN’ encoded as 25114. You could decode that in many different ways!”
Alice: "Sure you could, but what words would you get? Other than ‘BEAN’, you’d get ‘BEAAD’, ‘YAAD’, ‘YAN’, ‘YKD’ and ‘BEKD’. I think you would be able to figure out the correct decoding. And why would you send me the word ‘BEAN’ anyway?”
Bob: “OK, maybe that’s a bad example, but I bet you that if you got a string of length 500 there would be tons of different decodings and with that many you would find at least two different ones that would make sense.”
Alice: “How many different decodings?”
Bob: “Jillions!”
For some reason, Alice is still unconvinced by Bob’s argument, so she requires a program that will determine how many decodings there can be for a given string using her code.
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
Source
East Central North America 2004
问题链接:POJ2033 LA3078 HDU1508 ZOJ2202 Alphacode
问题简述:(略)
问题分析:简单题,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)
AC的C++语言程序(记忆化递归)如下:
/* POJ2033 LA3078 HDU1508 ZOJ2202 Alphacode */
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const int N = 1e5 + 2;
char s[N];
int len;
LL dp[N];
LL dfs(int k)
{
if (k >= len - 1) return 1;
else if (dp[k]) return dp[k];
if (s[k + 1]) dp[k] += dfs(k + 1);
if (s[k] * 10 + s[k + 1] <= 26 && s[k + 2]) dp[k] += dfs(k + 2);
return dp[k];
}
int main()
{
while (~scanf("%s", s) && strcmp(s, "0") != 0) {
len = strlen(s);
for (int i = 0; i < len; i++) s[i] -= '0';
s[len] = 1;
memset(dp, 0, sizeof dp);
printf("%lld\\n", dfs(0));
}
return 0;
}
AC的C++语言程序(DP)如下:
/* POJ2033 LA3078 HDU1508 ZOJ2202 Alphacode */
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const int N = 1e5 + 2;
char s[N];
int len;
LL dp[N];
int main()
{
while (~scanf("%s", s) && strcmp(s, "0") != 0) {
len = strlen(s);
for (int i = 0; i < len; i++) s[i] -= '0';
s[len] = 1;
memset(dp, 0, sizeof dp);
dp[len] = 1;
for (int i = len - 1; i >= 0; i--)
if (s[i]) {
dp[i] = dp[i + 1];
if (s[i] * 10 + s[i + 1] <= 26)
dp[i] += dp[i + 2];
}
printf("%lld\\n", dp[0]);
}
return 0;
}
以上是关于POJ2033 LA3078 HDU1508 ZOJ2202 AlphacodeDFS+DP的主要内容,如果未能解决你的问题,请参考以下文章
HDU1416 POJ1078 UVA653 LA5507 GizilchDFS
POJ2142 LA3185 HDU1356 The Balance同余方程
POJ1323 LA2521 HDU1338 ZOJ1362 Game Prediction贪心
POJ2092 LA3157 HDU1347 ZOJ2250 Grandpa is Famous排序
POJ2704 HDU1208 LA3390 Pascal‘s Travels递推+记忆化搜索
UVA619 LA5465 POJ1312 HDU1314 ZOJ1272 Numerically Speaking大数+进制