Sicily 1001. Alphacode

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

25114
1111111111
3333333333
0

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;
}          
时间: 2024-11-08 09:50:44

Sicily 1001. Alphacode的相关文章

Sicily 1001

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 theirmessages: Alice: "Let's just use a very simple code: We'll assign `A' the

sicily 1001. Fibonacci 2

1001. Fibonacci 2 Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn-1 + Fn-2 for n ≥ 2. For example, the first ten terms of the Fibonacci sequence are: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, … Given an integer n, your goal is to co

Sicily 1001(dp)

题目连接:http://soj.sysu.edu.cn/1001 解题报告:f[i]表示第i个字母前能组成的种类 情况:1.s[i]==0,f[i]=f[i-1] 2.s[i-1]*10+s[i]>=10&&<=26,f[i]=f[i-1]+f[i-2] 3.!s[i-1]*10+s[i]>=10&&<=26,f[i]=f[i-1]; #include <iostream> #include <cstring> #includ

编程题目分类(剪辑)

1. 编程入门 2. 数据结构 3. 字符串 4. 排序 5. 图遍历 6. 图算法 7. 搜索:剪枝,启发式搜索 8. 动态规划/递推 9. 分治/递归 10. 贪心 11. 模拟 12. 算术与代数 13. 组合问题 14. 数论 15. 网格,几何,计算几何 [编程入门] PC 110101, uva 100, The 3n+1 problem, 难度 1 PC 110102, uva 10189, Minesweeper, 难度 1 PC 110103, uva 10137, The T

paste 乙级 1001

1001. 害死人不偿命的(3n+1)猜想 (15) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 卡拉兹(Callatz)猜想: 对任何一个自然数n,如果它是偶数,那么把它砍掉一半:如果它是奇数,那么把(3n+1)砍掉一半.这样一直反复砍下去,最后一定在某一步得到n=1.卡拉兹在1950年的世界数学家大会上公布了这个猜想,传说当时耶鲁大学师生齐动员,拼命想证明这个貌似很傻很天真的命题,结果闹得学生们无心学业

1001 数组中和等于K的数对

1001 数组中和等于K的数对 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 给出一个整数K和一个无序数组A,A的元素为N个互不相同的整数,找出数组A中所有和等于K的数对.例如K = 8,数组A:{-1,6,5,3,4,2,9,0,8},所有和等于8的数对包括(-1,9),(0,8),(2,6),(3,5). Input 第1行:用空格隔开的2个数,K N,N为A数组的长度.(2 <= N <= 50000,-10^9 <= K <= 10^9)

杭电女生赛1001 1002 1003 1005 1008 hdu6023 6024 6025 6027 6030

代码先贴这里 #include "iostream" #include "string.h" #include "stack" #include "queue" #include "string" #include "vector" #include "set" #include "map" #include "algorithm&quo

BZOJ 1001 [BeiJing2006]狼抓兔子

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1001 题意: ... 很容易想到求的是一个最小割=最大流. 之前一直用的刘汝佳的模板STL过题,很久没用过数组模拟了. 再次熟悉一下写法,first数组是索引数组,标记的结点的最后一条边,利用next数组找到上一条边. 1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 #define N 1100000 6 int n,m,

求最大边/最小边的比值最小的路径 codevs 1001 舒适的路线

codevs 1001 舒适的路线 2006年 时间限制: 2 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description Z小镇是一个景色宜人的地方,吸引来自各地的观光客来此旅游观光.Z小镇附近共有N(1<N≤500)个景点(编号为1,2,3,…,N),这些景点被M(0<M≤5000)条道路连接着,所有道路都是双向的,两个景点之间可能有多条道路.也许是为了保护该地的旅游资源,Z小镇有个奇怪的规定,就是对于一条给定的公路Ri,任何在该公路上行驶的车