您的位置:首页 > 其它

Sicily 1001. Alphacode

2015-03-31 00:25 218 查看

1001. Alphacode

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

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

[code]
25114
1111111111
3333333333
0
[/code]

Sample Output

6
89
1
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;

bool is_letter(char front, char back) {//判断当前字符能否与前面的字符组成新字符
    if (front >= '3' || front == '0')//注意不会有01表示A的情况(不出现前导零)
        return false;
    else if (front =='1')
        return true;
    else if (back > '6')
        return false;
    else
        return true;
}

int main() {
    
    char num[10000];
    int i;
    long long code_num[10000];
    
    while (scanf("%s", num) && num[0] != '0') {
        
        memset(code_num, 0, sizeof(code_num));
        
        code_num[0] = 1;
        if (is_letter(num[0], num[1]) && num[1] != '0') {//特殊处理,注意当num[1] == '0'的时候只能跟前面的字符组成新字符,不能单独表示字符
            code_num[1] = 2;
        } else {
            code_num[1] = 1;
        }
        
        for (i = 2; i < (int)strlen(num); i++) {
            if (num[i] == '0')//假如是零必然要跟前面的组成新字符,相当于处理num[i - 1]的时候num[i - 1]只能单独表示一个字符的情况
                code_num[i] = code_num[i - 2];
            else if (is_letter(num[i - 1], num[i]))//假如可以,一种是单独字符,一种是组合字符
                code_num[i] = code_num[i - 2] + code_num[i - 1];
            else//这是只能单独字符的情况
                code_num[i] = code_num[i - 1];
        }
        printf("%lld\n", code_num[(int)strlen(num) - 1]);
    }
    return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: