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时,默 ...
随机推荐
- linux命令行下常用快捷键
快捷键的使用: ctrl+d或者使用logout.exit退出终端ctrl+a跳到开始ctrl+e跳到最后ctrl+u向前删除ctrl+k向后删除ctrl+c中断命令ctrl+z暂停命令 fg:将暂停 ...
- Web23_Listener
记得在web.xml配置<listener-class>监听器的Copy Qualified Name复制类全名</listener-class> <listener-c ...
- 007. Reverse Integer
题目链接:https://leetcode.com/problems/reverse-integer/description/ Given a 32-bit signed integer, rever ...
- 升级Nginx1.14.1以上版本
一.编译Nginx ①.下载Nginx最新版 目前Nginx最新版是今年11月份发布的 1.9.7 版本,反正1.9+是没有稳定版,所以干脆弄个最新版: cd /usr/local/src w ...
- linux(centos7)下SVN服务器搭建
https://www.cnblogs.com/fuyuanming/p/6123395.html linux(centos)下SVN服务器如何搭建?说到SVN服务器,想必大家都知道,可以是在LINU ...
- Metinfo5.1 /include/common.php 变量覆盖+SQL注入漏洞
- linux centos 安装输入法
终端输入命令: yum install ibus-libpinyin.x86_64
- elk 入门 - 分析nginx日志 + json格式 + 有调试的意识 + elk7.2.0
1.本次采用的一台主机,将所有的软件安装一台上进行测试工作. 2.安装部署:https://blog.51cto.com/hwg1227/2299995 3.简单调试 输出rubydebug inpu ...
- C++ unsigned long 转化为 unsigned char*
C++ Code 123456789101112131415161718 unsigned long lFileLen = 1000; unsigned char *ucFileLenFlag; ...
- java暂停线程
暂停线程 本节介绍两个被废弃的用于线程暂停和恢复的方法suspend().resume().主要探究废弃原因,强调线程的安全性.主要有两个原因 原因1: suspend().resume()使用不当, ...