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. 约瑟夫环之循环链表实现
  2. 数据单位:bit、Byte、KB、MB、GB、TB、PB、EB、ZB、YB、BB、NB、DB、、、
  3. c语言计算坐标三角形面积公式,c语言计算三角形面积代码
  4. 计算机网络项目——最小网元设计(阶段二)
  5. 小汤学编程之JAVA基础day10——常用类(二):String常用方法、正则、StringBuffer和StringBuilder、Math和Random类、日期类和数字类
  6. 一分钟了解阿里云产品:高速通道
  7. 光模块组装过程中常见问题分析及解决方法
  8. mac 2018 idea 无法 import导入或打开maven 项目
  9. C++STL优先队列使用
  10. 大佬们用代码写的故事
  11. 计算理论导论第1章答案 Michael Sipser
  12. 解决连接kudu时,delaying RPC due to Service unavailable: Master config (**.**.**.**:7051) has no leader
  13. 卓有成效的管理者(笔记)——要事优先
  14. 【重识云原生】第六章容器6.3.5节——Controller Manager概述
  15. 前端面试题汇总(JavaScript面试纯干货)
  16. 互联网短信网关接口协议(V3.0.0)
  17. Linux 安装仿宋字体
  18. 163邮箱如何申请注册个人?163电子邮箱个人怎么注册?
  19. 计算机二级数据库mysql题库_全国计算机二级mysql数据库模拟试题
  20. 多维度Mysql数据库优化策略 (建议收藏)!!

热门文章

  1. ubuntu-18.10 允许 root登录图形界面
  2. 和谐: OρenVΡN的UDP握手协议
  3. 一张纸厚度是多少毫米_一张纸对折后的厚度,有多可怕!——北京市第二十中学教科室“科技云课堂”(4)...
  4. Arcgis javascript那些事儿(十六)——GP服务的发布与使用
  5. js请求后台接口返回的图片并转为base64
  6. apache服务器进程配置文件是,apache服务器进程配置文件是
  7. linux grep跨行文本匹配,grep跨行匹配
  8. Kylin兼容性问题解决
  9. 执行 Python 程序的三种方式及Python 的 IDE —— `PyCharm`
  10. 详解:hive启动hiveserver2连JDBC报错:Could not open client transport with JDBC Uri 解决方案