zoukankan      html  css  js  c++  java
  • poj 2033 Alphacode (dp)

    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]);
            }
        }
    }
  • 相关阅读:
    AX2009使用NPOI导出EXCEL颜色设置
    AX2009使用NPOI导出EXCEL常用格式设置
    AX2009使用NPOI导出EXCEL2007
    AX2009使用NPOI导出EXCEL
    【基础复习】五:STL模板与容器
    【基础复习】四:循环、递归与概率
    【基础复习】三:指针与引用
    【基础复习】二:预处理、const与sizeof
    【基础复习】一:程序设计基本概念
    TEC-2 机 微程序设计
  • 原文地址:https://www.cnblogs.com/kkkkkk/p/5546707.html
Copyright © 2011-2022 走看看