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 Alphacode【DFS+DP】相关推荐

  1. 【数位DP】恨7不成妻

    [数位DP]恨7不成妻 时间限制: 1 Sec  内存限制: 128 MB 提交: 8  解决: 4 [提交] [状态] [命题人:admin] 题目描述 单身! 依然单身! 吉哥依然单身! DS级码 ...

  2. 【概率DP】$P2059$ 卡牌游戏

    [概率DP]P2059 卡牌游戏 链接 题目描述 N个人坐成一圈玩游戏.一开始我们把所有玩家按顺时针从1到N编号.首先第一回合是玩家1作为庄家.每个回合庄家都会随机(即按相等的概率)从卡牌堆里选择一张 ...

  3. Bailian4004 数字组合【递归+DP】

    4004:数字组合 总时间限制: 1000ms 内存限制: 65536kB 描述 有n个正整数,找出其中和为t(t也是正整数)的可能的组合方式.如: n=5,5个数分别为1,2,3,4,5,t=5: ...

  4. UVA497 Strategic Defense Initiative【LIS+DP】

    "Commander! Commander! Please wake up commander!"     "- mmmph. What time is it?" ...

  5. Bailian2755 神奇的口袋【递归+DP】

    2755:神奇的口袋 总时间限制: 10000ms 内存限制: 65536kB 描述 有一个神奇的口袋,总的容积是40,用这个口袋可以变出一些物品,这些物品的总体积必须是40.John现在有n个想要得 ...

  6. 20天拿下华为OD笔试之【DFS/BFS】2023Q1A-开心消消乐【闭着眼睛学数理化】全网注释最详细分类最全的华为OD真题题解

    [DFS/BFS]2023Q1A-开心消消乐 题目描述与示例 题目描述 给定一个 N 行 M 列的二维矩阵,矩阵中每个位置的数字取值为 0 或 1,矩阵示例如: 1 1 0 0 0 0 0 1 0 0 ...

  7. 【限时免费】20天拿下华为OD笔试之【DFS/BFS】2023B-寻找最大价值的矿堆【闭着眼睛学数理化】全网注释最详细分类最全的华为OD真题题解

    [DFS/BFS]2023B-寻找最大价值的矿堆 题目描述与示例 给你一个由 '0'(空地).'1'(银矿).'2'(金矿)组成的的地图,矿堆只能由上下左右相邻的金矿或银矿连接形成.超出地图范围可以认 ...

  8. 【动态规划dp】青蛙的烦恼(frog)

    青蛙的烦恼(frog) [题目描述] 池塘中有 n 片荷叶恰好围成了一个凸多边形,有一只小青蛙恰好站在 1 号荷叶上,小青蛙想通过 最短的路程遍历所有的荷叶(经过一个荷叶一次且仅一次),小青蛙可以从一 ...

  9. 【搜索+DP】codevs1066-引水入城

    [题目大意] 一个N行M列的矩形,如上图所示,其中每个格子都代表一座城 市,每座城市都有一个海拔高度.现在要在某些城市建造水利设施.水利设施有两种,分别为蓄水厂和输水站.蓄水厂的功能是利用水泵将湖泊中 ...

最新文章

  1. 来看看如何使用策略模式干掉讨厌的 if else
  2. 浅谈Javascript中的void操作符
  3. Go to sleep
  4. 【人脸表情识别】情绪识别相关会议、比赛汇总(2018-2020)
  5. 困难时拉你一把的图片_2019早上好表情图片合集 早晨祝福语动画
  6. Linux 用户(user)和用户组(group)管理概述
  7. 入侵检测规则匹配算法--单模匹配算法、多模匹配算法、hyperscan
  8. 升级换代!Facebook全新电商搜索系统Que2Search
  9. ANSYS12.0安装教程
  10. 递归算法php,php递归算法经典实例
  11. 华为薪资等级结构表_华为公司等级薪酬制度
  12. Cocos Creator三消小游戏(TS 02版)
  13. 2022,从阅读开始
  14. Vue watch 监听复杂对象变化,oldvalue 和 newValue 一致的解决办法。
  15. OCJP 1Z0-808考题解析 题31--60
  16. windows环境安装lzo和python-lzo
  17. 已经建立的TCP,收到SYN会发生什么?
  18. 语音和面部识别技术能帮助AI在情商上超越人类吗
  19. php7随机数random_int()
  20. linux db2扩展表空间,Linux/ext3:DB2扩展表空间导致的Latch等待现象1

热门文章

  1. [JavaME]利用java.util.TimerTask来做Splash Screen的N种方法
  2. 2019-11-24转载Ganglia 安装和使用
  3. 2018-03-22笔记,象棋记谱法
  4. MUI+Htmlplus开发APP实现页面之间传值
  5. Laravel的核心概念
  6. 基于进程的游戏Server端架构设计
  7. mysql导入txt linux_Linux中将txt导入到mysql的方法教程
  8. 【java学习之路】(java框架)008.JdbcTemplate
  9. Linux防火墙开关命令
  10. hive 中String对长度没有限制