题干:

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

解题报告:

有点坑啊,,直接dp[i]=dp[i-1]了,,殊不知,如果这一位为0的话,,就得dp[i]=dp[i-2]并且接下来的操作都不能参与了,也就是得直接continue才行、、、

思路就是首先初始化成dp[i-1]的值(当然s[i]=='0'就另说了),然后xjb转移一下就好了。。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
ll dp[MAX];
char s[MAX];
int main()
{while(cin>>(s+1)) {memset(dp,0,sizeof dp);int len = strlen(s+1);dp[0]=1;if(s[1] != '0') dp[1]=1;else break;for(int i = 2; i<=len; i++) {if(s[i] == '0') {dp[i] = dp[i-2];continue;}else dp[i] = dp[i-1];if(s[i-1] == '1') dp[i] += dp[i-2];if(s[i-1] == '2' && s[i] <= '6') dp[i] += dp[i-2];if(s[i] == '0' && (s[i-1]=='0'||s[i-1]>'2')) dp[i]=0;}printf("%lld\n",dp[len]);}//10110return 0 ;}

AC代码2:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
ll dp[MAX];
char s[MAX];
int main()
{while(cin>>(s+1)) {memset(dp,0,sizeof dp);int len = strlen(s+1);dp[0]=1;if(s[1] != '0') dp[1]=1;else break;for(int i = 2; i<=len; i++) {dp[i] = dp[i-1];if(s[i-1] == '1') dp[i] += dp[i-2];if(s[i-1] == '2' && s[i] <= '6') dp[i] += dp[i-2];if(s[i] == '0' && (s[i-1]=='0'||s[i-1]>'2')) dp[i]=0;if(s[i] == '0') dp[i] = dp[i-2];}printf("%lld\n",dp[len]);}//10110return 0 ;}

错误代码1:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
ll dp[MAX];
char s[MAX];
int main()
{while(cin>>(s+1)) {memset(dp,0,sizeof dp);int len = strlen(s+1);dp[0]=1;if(s[1] != '0') dp[1]=1;else break;for(int i = 2; i<=len; i++) {if(s[i] == '0') dp[i] = dp[i-2];else dp[i] = dp[i-1];if(s[i-1] == '1') dp[i] += dp[i-2];if(s[i-1] == '2' && s[i] <= '6') dp[i] += dp[i-2];if(s[i] == '0' && (s[i-1]=='0'||s[i-1]>'2')) dp[i]=0;}printf("%lld\n",dp[len]);}//10110return 0 ;}

总结:

这题dp[0]=1是必须要赋值的,,因为你有dp[i] += dp[i-2],所以  i==2 的时候,需要用到dp[0]的状态。

【 POJ - 2033 】Alphacode (dp,有坑)相关推荐

  1. Fire (poj 2152 树形dp)

    Fire (poj 2152 树形dp) 给定一棵n个结点的树(1<n<=1000).现在要选择某些点,使得整棵树都被覆盖到.当选择第i个点的时候,可以覆盖和它距离在d[i]之内的结点,同 ...

  2. poj 1925 Spiderman (dp)(疯狂TLE)

    题目链接:http://poj.org/problem?id=1925 题意:给出N个点,每个点都有两个数x.y,其中x表示点的横坐标,y表示纵坐标(建筑物的高度):给出的每个点都满足y值是大于等于起 ...

  3. POJ 图论分类 + DP(较全 自己又加了点)

    DP -----------动态规划 状态压缩DP 2411 (棋盘规模较大)状态压缩DP+DFS+滚动数组 2664 (棋盘规模较小)直接递推即可(DP) 2506 (棋盘规模较小)直接递推即可(D ...

  4. POJ 2096 (概率DP)

    题目链接: http://poj.org/problem?id=2096 题目大意:n种bug,s个子系统.每天随机找一个bug,种类随机,来自系统随机.问找齐n种bug,且每个子系统至少有一个bug ...

  5. POJ 2955 (区间DP)

    题目链接: http://poj.org/problem?id=2955 题目大意:括号匹配.对称的括号匹配数量+2.问最大匹配数. 解题思路: 看起来像个区间问题. DP边界:无.区间间隔为0时,默 ...

  6. 目的地返回POJ 2336 动态规划(DP) Ferry Loading II

    在写这篇文章之前,xxx已写过了几篇关于改目的地返回主题的文章,想要了解的朋友可以去翻一下之前的文章 标题链接:http://poj.org/problem?id=2336 分析:想设我们要求的是第i ...

  7. poj 2355(简单dp)

    题意:求两个车站之间的最小费用. 解题思路:这道题本来应该是属于线段树优化的dp问题,但是数据太水了,所以O(n²)的复杂度也过了.主要是线段树的话难得打,所以就直接用暴力破了.这里有个坑:a可能大于 ...

  8. POJ 2342 (树形DP)

    题目链接: http://poj.org/problem?id=2342 题目大意:直属上司和下属出席聚会.下属的上司出现了,下属就不能参加,反之下属参加.注意上司只是指直属的上司.每个人出席的人都有 ...

  9. POJ 3252 数位DP

    链接: http://poj.org/problem?id=3252 题意: 给你一个区间l,r,求区间中有多少个数转化为二进制后1的个数大于等于0的个数 题解: 还是数位dp,不过多了前导0的判断 ...

最新文章

  1. [转] linux系统文件流、文件描述符与进程间关系详解
  2. 数组去重是面试中经常问到的问题
  3. C++返回字符串函数的几种实现方法
  4. java4android (static关键字的作用)
  5. 函数_方法_的四种调用方式
  6. Teradata天睿公司推出适用各种部署环境的全球最强分析数据库
  7. 【51单片机快速入门指南】6.4:DHT11、DHT22单总线温湿度传感器
  8. Win7 单机Spark和PySpark安装
  9. linux 用户管理 指令,Linux 用户管理常用命令
  10. 苹果mac需牢记的SSH命令
  11. 计算机基础-计算机系统的安装
  12. 地图根据地名批量标注地点 发现好像要么限制了10-20个个数 要么就需要企业付费...
  13. PreScan、Carsim、Carmaker和VTD联合仿真
  14. 用VC2013编译了一个程序,在Windows 8、Windows 7(64位、32位)下都能正常运行。但在Win XP,Win2003下运行时,却报错不能运行
  15. a与a的共轭转置相乘_线性代数A矩阵乘以A的转置的含义或者几何意义
  16. 国外计算机论文翻译,计算机论文外文翻译
  17. 电子计算机用户网络新词秀,网络新词的研究
  18. UCI-HAR数据集CNN分类
  19. 重庆html5全景,用pano2vr 转swf 全景图
  20. Mac版Sublime Text3搭建c语言环境

热门文章

  1. [密码学基础][每个信息安全博士生应该知道的52件事][Bristol52]49.描述在IPsec和TLS后的基本想法
  2. [密码学基础][每个信息安全博士生应该知道的52件事][Bristol Cryptography][第32篇]基于博弈的证明和基于模拟的证明
  3. [Leedcode][JAVA][第210 题][课程表 II][拓扑排序][BFS][DFS][有向图]
  4. linux修改挂载目录名字,linux下修改mount挂载目录名
  5. 视图添加字段_使用ExploreByTouchHelper辅助类为自定义视图添加虚拟视图
  6. B. The Cake Is a Lie
  7. mfc 设置子窗口只打开一遍_MFC 判断子窗体是不是已经打开,避免重复创建
  8. python 属性描述符_Python属性描述符(二)
  9. 量产之后计算机读不出u盘,求大神、我量产没成功然后U盘就电脑上就不显示了...
  10. UE4多线程任务系统详解