SPOJ-394-ACODE - Alphacode / dp
ACODE - Alphacode
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 5000
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 at most 5000 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 64 bit signed integer.
Example
Input: 25114
1111111111
3333333333
0 Output: 6
89
1
递推方程很容易得到,一个重要的问题是对'0'的处理,譬如 "90" "1001" ,显然这些都是非法数据,我们在处理时要当心。
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long LL;
char s[];
LL f[];
int main()
{
int n,i,j,k;
while(){
scanf("%s",s+);
n=strlen(s+);
if(n == && s[] == '') break;
f[]=;
for(i=;i<=n;++i){
f[i]=;
if(s[i]!='') f[i]+=f[i-];
if(i> && s[i-]=='') f[i]+=f[i-];
if(i> && s[i-]=='' && s[i]>=''&& s[i]<='') f[i]+=f[i-];
}
cout<<f[n]<<endl;
}
return ;
}
SPOJ-394-ACODE - Alphacode / dp的更多相关文章
- 【BZOJ3769】spoj 8549 BST again DP(记忆化搜索?)
[BZOJ3769]spoj 8549 BST again Description 求有多少棵大小为n的深度为h的二叉树.(树根深度为0:左右子树有别:答案对1000000007取模) Input 第 ...
- spoj 394
每段可以连续的串的可能性是个Fibonacci数列 但是直接dp更好吧~~ #include <cstdio> #include <cstring> using names ...
- spoj Balanced Numbers(数位dp)
一个数字是Balanced Numbers,当且仅当组成这个数字的数,奇数出现偶数次,偶数出现奇数次 一下子就相到了三进制状压,数组开小了,一直wa,都不报re, 使用记忆化搜索,dp[i][s] 表 ...
- [spoj Favorite Dice ][期望dp]
(1)https://vjudge.net/problem/SPOJ-FAVDICE 题意:有一个n面的骰子,每一面朝上的概率相同,求所有面都朝上过至少一次的总次数期望. 题解:令dp[i]表示 i ...
- poj 2033 Alphacode (dp)
Alphacode Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 13378 Accepted: 4026 Descri ...
- SPOJ 423 Assignments 状态DP
这个题目搁置了这么久,终于搞完了. 给n个人分配n个课程,已经告诉了你n个人对哪几门感兴趣,问最多有多少种分配方式 我刚开始都没找到这怎么还可以状态dp,哪来的状态转移,想用暴力DFS,果断TLE的妥 ...
- SPOJ Favorite Dice(概率dp)
题意: 一个骰子,n个面,摇到每一个面的概率都一样.问你把每一个面都摇到至少一次需要摇多少次,求摇的期望次数 题解: dp[i]:已经摇到i个面,还需要摇多少次才能摇到n个面的摇骰子的期望次数 因为我 ...
- spoj 1812 LCS2(SAM+DP)
[题目链接] http://www.spoj.com/problems/LCS2/en/ [题意] 求若干个串的最长公共子串. [思路] SAM+DP 先拿个串建个SAM,然后用后面的串匹配,每次将所 ...
- 【SPOJ 2319】 BIGSEQ - Sequence (数位DP+高精度)
BIGSEQ - Sequence You are given the sequence of all K-digit binary numbers: 0, 1,..., 2K-1. You need ...
随机推荐
- 论MYSQL数据库数据错误的处理
1,备份 2,事务回滚 3,binlog日志回复 4,以上措施都没有,那就望洋兴叹吧
- contentSize、contentInset和contentOffset 是 scrollView三个基本的属性区别和使用
contentSize.contentInset和contentOffset 是 scrollView三个基本的属性. contentSize: 其实就是scrollview可以滚动的区域,比如fra ...
- find()函数
find()函数返回类型:size_type 1/S.find(T):返回T在S中第一次匹配的下标位置 2/S.find_first_of(T):返回字符串T第一个字符在S中第一次出现的下标位置 3/ ...
- 基于TSUNG对MQTT进行压力测试-基础概念温习
[单台Broker压测结果]请移步另一篇博客:http://www.cnblogs.com/lingyejun/p/7941271.html 一.TCP报头部中的SYN.FIN.ACK: ACK : ...
- POJ - 2125 Destroying The Graph (最小点权覆盖)
题意:给一张图,现在要删去所有的边,删去一个点的所有入边和所有出边都有其对应\(W_{i+}\)和\(W_{i-}\).求删去该图的最小花费,并输出解 分析:简而言之就是用最小权值的点集去覆盖所有的边 ...
- 并发队列ConcurrentLinkedQueue与阻塞队列LinkedBlockingQueue的区别
1. 介绍背景 在Java多线程应用中,队列的使用率很高,多数生产消费模型的首选数据结构就是队列. Java提供的线程安全的Queue可以分为阻塞队列和非阻塞队列,其中阻塞队列的典型例子是Block ...
- 字符编码的发展(ASCII、Unicode、utf-8)
最近一直在看廖雪峰老师的python网上教程,python内容简单易理解,就没整理,但是字符串编码作为一直困扰自己的问题,看了几遍文章,最终还是将其整理如下,本篇博客总结自廖雪峰老师的网上教程:htt ...
- tomcat源码调试
三.tomcat目录结构 tomcat的下载安装有很多教程,不再赘述. 现在的tomcat已经到9了,当tomcat下载安装完成后,其目录大致如下: 除了上面的文件夹,还有四个文件: ...
- ThinkPHP5显示数据库字段内容
1.在application文件夹下面的config.php中打开DEBUG. 2.修改tp5/application/index/controller/Index.php内容. <?php n ...
- Python for循环文件
for 循环遍历文件:打印文件的每一行 #!/usr/bin/env python fd = open('/tmp/hello.txt') for line in fd: print line, 注意 ...