HDU 3689 Infinite monkey theorem [KMP DP]
Infinite monkey theorem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1702 Accepted Submission(s):
882
Surely monkeys are smart among animals. But their limited intelligence is no
match for our human beings. However, there is a theorem about monkeys, and it
states that monkeys can write everything if given enough time.
The theorem is
called “Infinite monkey theorem”. It states that a monkey hitting keys at random
on a typewriter keyboard for an infinite amount of time will almost surely type
any given text, which of course includes the programs you are about to write
(All computer programs can be represented as text, right?).
It’s very easy to
prove this theorem. A little calculation will show you that if the monkey types
for an infinite length of time the probability that the output contains a given
text will approach 100%.
However, the time used is too long to be physically
reasonable. The monkey will not be able to produce any useful programs even if
it types until the death of the universe. To verify this and ensure that our
human beings are not replaceable by monkeys, you are to calculate the
probability that a monkey will get things right.
Each test case
begins with a line containing two integers n and m separated by a whitespace
(2<=n<=26, 1<=m<=1000). n is the number of keys on the typewriter
and the monkey will hit these keys m times. Thus the typewriter will finally
produce an output of m characters.
The following n lines describe keys on the
typewriter. Each line has a lower case letter and a real number separated by a
whitespace. The letter indicates what the typewriter will produce if the monkey
hits that key and the real number indicates the probability that the monkey will
hit this key. Two hits of the monkey are independent of each other (Two
different hits have the same probability for a same key), and sum of all the
probabilities for each key is ensured to be 1.
The last line of the test case
contains a word composed of lower case letters. The length of the word will be
less than or equal to 10.
The input will end with a line of two zeros
separated by a whitespace. This line should not be processed.
probability that the given word will appear in the typewriter’s output. The
output should be in percentage format and numbers should be rounded to two
digits after the decimal point.
题意:
字符集中有一些字符(最多26个),给出每个字符的出现概率(它们的和保证为1)
再给出一个子串B
求:任给一个长度为N的字符串A(只能包含字符集中的字符),使得B是A的子串的概率。
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long ll;
const int N=,M=;
int cn,n,m,fail[N];
char c[],b[N],key[N];
double p[N],f[N][M];
void getFail(int n,char s[]){
fail[]=;
for(int i=;i<=n;i++){
int j=fail[i-];
while(j&&s[j+]!=s[i]) j=fail[j];
fail[i]=s[j+]==s[i]?j+:;
}
}
void dp(){
memset(f,,sizeof(f));
f[][]=;
for(int i=;i<n;i++)
for(int j=;j<m;j++) if(f[i][j])
for(int k=;k<=cn;k++){
int now=j;
while(now&&b[now+]!=key[k]) now=fail[now];
now+=b[now+]==key[k];
f[i+][now]+=f[i][j]*p[k];
}
double ans=;
for(int i=;i<=n;i++) ans+=f[i][m];
ans*=;
printf("%.2lf%%\n",ans);
}
int main(){
//freopen("in.txt","r",stdin);
while(scanf("%d%d",&cn,&n)!=EOF&&(cn||n)){
for(int i=;i<=cn;i++){
scanf("%s%lf",c,&p[i]);
key[i]=c[];
}
scanf("%s",b+);
m=strlen(b+);
getFail(m,b);
dp();
}
}
HDU 3689 Infinite monkey theorem [KMP DP]的更多相关文章
- HDU 3689 Infinite monkey theorem(DP+trie+自动机)(2010 Asia Hangzhou Regional Contest)
Description Could you imaging a monkey writing computer programs? Surely monkeys are smart among ani ...
- [HDU 3689]Infinite monkey theorem (KMP+概率DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3689 黄老师说得对,题目只有做wa了才会有收获,才会有提高. 题意:一个猴子敲键盘,键盘上有n个键,猴 ...
- hdu 3689 Infinite monkey theorem
Infinite monkey theorem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...
- HDU 3689 Infinite monkey theorem ——(自动机+DP)
这题由于是一个单词,其实直接kmp+dp也无妨.建立自动机当然也是可以的.设dp[i][j]表示匹配到第i个字母的时候,在单词中处于第j个位置的概率,因此最终的答案是dp[0~m][len],m是输入 ...
- [AC自己主动机+可能性dp] hdu 3689 Infinite monkey theorem
意甲冠军: 给n快报,和m频率. 然后进入n字母出现的概率 然后给目标字符串str 然后问m概率倍的目标字符串是敲数量. 思维: AC自己主动机+可能性dp简单的问题. 首先建立trie图,然后就是状 ...
- ●HDU 3689 Infinite monkey theorem
题链: http://acm.hdu.edu.cn/showproblem.php?pid=3689题解: KMP,概率dp (字符串都从1位置开始) 首先对模式串S建立next数组. 定义dp[i] ...
- hdu 3689 杭州 10 现场 J - Infinite monkey theorem 概率dp kmp 难度:1
J - Infinite monkey theorem Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d &am ...
- hdu-3689 Infinite monkey theorem 概率dp+kmp
有一只猴子随机敲键盘,给出它可能敲的键以及敲各个键的概率. 输入:n,表示有多少个键,m,表示猴子会敲m次键 n个二元组(字母,数字) 表示键代表的字母及其被敲的概率. 最后一个目标字符串. 问这只猴 ...
- HUD3689 Infinite monkey theorem
Infinite monkey theorem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...
随机推荐
- oracle 11g体系结构
1.数据库体系结构 1.1 数据库和实例的关系database server = instance 实例 + database 数据库instance 实例 =内存 memory + 进程 pr ...
- css3渐变之径向渐变
径向渐变由它的中心定义.可以指定渐变的中心.形状(原型或椭圆形).大小.默认情况下,渐变的中心是 center(表示在中心点),渐变的形状是 ellipse(表示椭圆形),渐变的大小是 farthes ...
- HDU 4763 Theme Section
题目: It's time for music! A lot of popular musicians are invited to join us in the music festival. Ea ...
- DTD约束
DTD约束 一,导入DTD方式 二,DTD语法 2)DTD语法 约束标签 <!ELEMENT 元素名称类别>或<!ELEMENT 元素名称(元素内容)> 类别: 空标签: ...
- 一步步部署基于Windows系统的Jenkins持续集成环境
如题:本文将介绍如何在Windows环境下运用Jenkins部署持续集成环境.之所以写本文,是因为在最近工作当中,学习使用Jenkins时,确实遇到了一些问题,而大多数教程文档都是基于Mac或是Lin ...
- kafka和strom集群的环境安装
前言 storm和kafka集群安装是没有必然联系的,我将这两个写在一起,是因为他们都是由zookeeper进行管理的,也都依赖于JDK的环境,为了不重复再写一遍配置,所以我将这两个写在一起.若只需一 ...
- [知了堂学习笔记]_css3特效第一篇--旋转的背景&翻书效果
一.html遮盖层与css3的旋转动画 >效果图(加载可能会慢一点儿,请稍等...): >实现思路:在一个大的div中装入一个底层img和顶层的div(里面的内容按照以上图片呈现的样式布局 ...
- Java中实例方法、类方法和构造方法
类方法,有static修饰符,典型的主函数public static void main(String[] args){}实例方法,就是一般的方法构造方法,没有返回值(就是连void都没有),方法名与 ...
- 美国不同C段服务器,多ip服务器
作为多IP服务器的拓展,多C段服务器,例如:IP分为4段,A段,B段,C段,D段.192.168.0.1/24代表着一个C段,可用IP段为192.168.0.1-255,一个C段有253个可用IP.一 ...
- spring使用@Cache的简单实现
基于xml的配置感觉没有注解形式简单明了,咱不考虑了. 进入正题之前先提个疑问,希望知道的人能告诉一下 下述介绍会有这段代码: @Cacheable(value="myCache" ...