poj 2033 Alphacode (dp)
| Time Limit: 1000MS | Memory Limit: 30000K | |
| Total Submissions: 13378 | Accepted: 4026 |
Description
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
Output
Sample Input
25114
1111111111
3333333333
0
Sample Output
6
89
1
分析:因为题目给的都是正常数据,所以不会出现两个0连在一起的情况。这道题要十分注意有0出现的情况(因为0 WA了好几次。。,我本来以为题目中会有非合法的情况出现的,比如1002,WA之后还把02当做一个数也试了试,依旧WA。。看了discuss的测试数据才知道这么想是错的),当有0出现时,说明前面的那个数肯定是1或2,可以和这个0配对,所以result[i] = result[i - 2]。对于非0 的情况,如果前面的数是1,则当前的数可以单独作为一个数存在(result[i - 1]种情况),也可以和前面的1配对存在(result[i - 2]种情况),当前面的数是2,当前的数大于0小于7时也是这种情况。否则的话就是只能当前的数作为单独的一个数存在了,有result[i - 1]种情况。
Java AC 代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = "";
char[] input;
long[] result;
while(!(str = sc.next()).equals("0")) {
input = str.toCharArray();
int len = input.length;
result = new long[len];
result[0] = 1;
if(len == 1) {
System.out.println(1);
continue;
}
if(input[1] == '0')
result[1] = 1;
else if(input[0] == '1' || input[0] == '2' && input[1] < '7')
result[1] = 2;
else
result[1] = 1;
for(int i = 2; i < len; i++) {
if(input[i] == '0')
result[i] = result[i - 2];
else if(input[i - 1] == '1' || input[i - 1] == '2' && input[i] < '7')
result[i] = result[i - 2] + result[i - 1];
else
result[i] = result[i - 1];
}
System.out.println(result[len - 1]);
}
}
}
poj 2033 Alphacode (dp)的更多相关文章
- POJ 2033 Alphacode
Alphacode Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 11666 Accepted: 3564 Descri ...
- Fire (poj 2152 树形dp)
Fire (poj 2152 树形dp) 给定一棵n个结点的树(1<n<=1000).现在要选择某些点,使得整棵树都被覆盖到.当选择第i个点的时候,可以覆盖和它距离在d[i]之内的结点,同 ...
- poj上的dp专题
更新中... http://poj.org/problem?id=1037 dp[i][j][0]表示序列长度为i,以j开始并且前两位下降的合法序列数目; dp[i][j][1]表示序列长度为i, 以 ...
- POJ 2096 (概率DP)
题目链接: http://poj.org/problem?id=2096 题目大意:n种bug,s个子系统.每天随机找一个bug,种类随机,来自系统随机.问找齐n种bug,且每个子系统至少有一个bug ...
- poj 1463(树形dp)
题目链接:http://poj.org/problem?id=1463 思路:简单树形dp,如果不选父亲节点,则他的所有的儿子节点都必须选,如果选择了父亲节点,则儿子节点可选,可不选,取较小者. #i ...
- poj 2486( 树形dp)
题目链接:http://poj.org/problem?id=2486 思路:经典的树形dp,想了好久的状态转移.dp[i][j][0]表示从i出发走了j步最后没有回到i,dp[i][j][1]表示从 ...
- poj 3140(树形dp)
题目链接:http://poj.org/problem?id=3140 思路:简单树形dp题,dp[u]表示以u为根的子树的人数和. #include<iostream> #include ...
- POJ 3661 (线性DP)
题目链接: http://poj.org/problem?id=3661 题目大意:牛跑步.有N分钟,M疲劳值.每分钟跑的距离不同.每分钟可以选择跑步或是休息.一旦休息了必须休息到疲劳值为0.0疲劳值 ...
- POJ 2955 (区间DP)
题目链接: http://poj.org/problem?id=2955 题目大意:括号匹配.对称的括号匹配数量+2.问最大匹配数. 解题思路: 看起来像个区间问题. DP边界:无.区间间隔为0时,默 ...
随机推荐
- 【SQL】MySQL---using的用法
学习记录: mysql中using的用法为: using()用于两张表的join查询,要求using()指定的列在两个表中均存在,并使用之用于join的条件
- WPF 键盘全局接收消息
1.========================================================================== 在c#中怎样禁用鼠标左键的使用,其实我们可以通 ...
- k8s报错日志查看
看系统日志 cat /var/log/messages 用kubectl 查看日志 # 注意:使用Kubelet describe 查看日志,一定要带上 命名空间,否则会报如下错误[root@node ...
- go中的数据结构-字典map
1. map的使用 golang中的map是一种数据类型,将键与值绑定到一起,底层是用哈希表实现的,可以快速的通过键找到对应的值. 类型表示:map[keyType][valueType] key一定 ...
- 01vscode配置git
一.准备工作 必须保证已安装git,相关安装git的教程很多,这里就不进行描述. 通过命令:git --version 查看git版本. 二.git config 配置 通过git config 配置 ...
- Oracle 行列转换公式
1.行转列 SELECT STU_NAME,TERM,ZHANBI,COURSE_MARK FROM (SELECT '罗飞' STU_NAME, '2001-2002' TERM, ' 微积分, ' ...
- 【Python开发】Python 适合大数据量的处理吗?
Python 适合大数据量的处理吗? python 能处理数据库中百万行级的数据吗? 处理大规模数据时有那些常用的python库,他们有什么优缺点?适用范围如何? 需要澄清两点之后才可以比较全面的看这 ...
- 封装自己的jquery插件
自己尝试封装的一个在工作当中使用的多级弹窗插件: ;(function ($, window, document) { //用一个自调用匿名函数把插架代码包裹起来,防止代码污染 $.fn.multi ...
- 蓝鲸 修改主机名重装后初始化不了cmdb安装不了job + 数据采集流程
1.表象:在部署蓝鲸JOB过程中需要进行RabbitMQ的安装,数据初始化,激活步骤,此问题多发生在此过程 [ root@rbtnodel install)# ./bkcec initdata rab ...
- elasticsearch 查询所有文档
0.添加一个索引 curl -i -XPUT http://172.31.250.16:10004/test_index/user/1 -d '{ "name": "小明 ...