Alphacode
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 13378   Accepted: 4026

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

分析:因为题目给的都是正常数据,所以不会出现两个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)的更多相关文章

  1. POJ 2033 Alphacode

    Alphacode Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 11666   Accepted: 3564 Descri ...

  2. Fire (poj 2152 树形dp)

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

  3. poj上的dp专题

    更新中... http://poj.org/problem?id=1037 dp[i][j][0]表示序列长度为i,以j开始并且前两位下降的合法序列数目; dp[i][j][1]表示序列长度为i, 以 ...

  4. POJ 2096 (概率DP)

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

  5. poj 1463(树形dp)

    题目链接:http://poj.org/problem?id=1463 思路:简单树形dp,如果不选父亲节点,则他的所有的儿子节点都必须选,如果选择了父亲节点,则儿子节点可选,可不选,取较小者. #i ...

  6. poj 2486( 树形dp)

    题目链接:http://poj.org/problem?id=2486 思路:经典的树形dp,想了好久的状态转移.dp[i][j][0]表示从i出发走了j步最后没有回到i,dp[i][j][1]表示从 ...

  7. poj 3140(树形dp)

    题目链接:http://poj.org/problem?id=3140 思路:简单树形dp题,dp[u]表示以u为根的子树的人数和. #include<iostream> #include ...

  8. POJ 3661 (线性DP)

    题目链接: http://poj.org/problem?id=3661 题目大意:牛跑步.有N分钟,M疲劳值.每分钟跑的距离不同.每分钟可以选择跑步或是休息.一旦休息了必须休息到疲劳值为0.0疲劳值 ...

  9. POJ 2955 (区间DP)

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

随机推荐

  1. SQL学习(六)select into:复制表信息

    在实际工作中,可能需要将一个表中的数据复制到另一个表中,以供后续使用.此时可以用关键字select into(在MySQL中不支持select into,可使用insert into...select ...

  2. delete file SVN commit error has no URL

    在提交SVN的时候遇到这个提交失败的提示: delete file SVN commit error has no URL 我的提交顺序是: 先在自己工程的文件夹删除 ->工程中删除 -> ...

  3. httplib/urllib实现

    httplib模块是一个底层基础模块,可以看到建立HTTP请求的每一步,但是实际的功能比较少.在python爬虫开发中基本用不到 下面详细介绍httplib提供的常用类型和方法: httplib.HT ...

  4. thinkphp6.0 nginx 配置

    location / {     index index.php;     #如果文件不存在则尝试TP解析     if (!-e $request_filename) {        rewrit ...

  5. 重学Python - Day 07 - python基础 -> linux命令行学习 -- 常用命令 一

    常用命令和使用方法如下: man man 命令 #可以查询命令的用法 cat 和 tac cat是正序显示文件内容 tac是倒叙显示文件内容 sort 对文件内容排序 uniq 忽略文件中重复行 hi ...

  6. B/S结构-登录页面-测试用例设计

    页面描述: 有一个登陆页面, 假如上面有2个textbox, 一个提交按钮 测试需求: 请针对这个页面设计30个以上的testcase 功能测试(Function test) 0. 什么都不输入,点击 ...

  7. OpenStack组件——Neutron网络服务(2)

    1.虚拟机获取 ip 1)用 namspace 隔离 DHCP 服务 Neutron 通过 dnsmasq 提供 DHCP 服务,而 dnsmasq 通过 Linux Network Namespac ...

  8. ctype.h头文件

    定义了一批C语言字符分类函数(C character classification functions),用于测试字符是否属于特定的字符类别,如字母字符.控制字符等等.既支持单字节(Byte)字符,也 ...

  9. json字段为null时输出空字符串

    Map < String , Object > jsonMap = new HashMap< String , Object>(); jsonMap.put(); jsonMa ...

  10. C++深拷贝和浅拷贝细节理解

    前提 在对象拷贝过程中,如果没有自定义拷贝构造函数,编译器会提供一个缺省的拷贝构造函数,缺省的拷贝构造函数对于基本类型的成员变量,按字节复制,对于类类型的成员变量则调用其相应的拷贝构造函数. 资料注解 ...