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

Problem Description
Could you imaging a monkey writing computer programs?
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.
Input
There will be several test cases.
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.
Output
For each test case, output one line containing the
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.
Source

题意:

字符集中有一些字符(最多26个),给出每个字符的出现概率(它们的和保证为1)
再给出一个子串B
求:任给一个长度为N的字符串A(只能包含字符集中的字符),使得B是A的子串的概率。


一边生成A,一边用KMP匹配B
f[i][j]表示生成到i位,当前匹配到B的j位的概率
枚举下一个字符,然后用KMP匹配就行了....匹配到now,就是f[i+1][now]+=f[i][j]*p[k]
#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]的更多相关文章

  1. 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 ...

  2. [HDU 3689]Infinite monkey theorem (KMP+概率DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3689 黄老师说得对,题目只有做wa了才会有收获,才会有提高. 题意:一个猴子敲键盘,键盘上有n个键,猴 ...

  3. hdu 3689 Infinite monkey theorem

    Infinite monkey theorem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/ ...

  4. HDU 3689 Infinite monkey theorem ——(自动机+DP)

    这题由于是一个单词,其实直接kmp+dp也无妨.建立自动机当然也是可以的.设dp[i][j]表示匹配到第i个字母的时候,在单词中处于第j个位置的概率,因此最终的答案是dp[0~m][len],m是输入 ...

  5. [AC自己主动机+可能性dp] hdu 3689 Infinite monkey theorem

    意甲冠军: 给n快报,和m频率. 然后进入n字母出现的概率 然后给目标字符串str 然后问m概率倍的目标字符串是敲数量. 思维: AC自己主动机+可能性dp简单的问题. 首先建立trie图,然后就是状 ...

  6. ●HDU 3689 Infinite monkey theorem

    题链: http://acm.hdu.edu.cn/showproblem.php?pid=3689题解: KMP,概率dp (字符串都从1位置开始) 首先对模式串S建立next数组. 定义dp[i] ...

  7. 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 ...

  8. hdu-3689 Infinite monkey theorem 概率dp+kmp

    有一只猴子随机敲键盘,给出它可能敲的键以及敲各个键的概率. 输入:n,表示有多少个键,m,表示猴子会敲m次键 n个二元组(字母,数字) 表示键代表的字母及其被敲的概率. 最后一个目标字符串. 问这只猴 ...

  9. HUD3689 Infinite monkey theorem

    Infinite monkey theorem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/ ...

随机推荐

  1. Android开发——BroadcastReceiver广播的使用

    想要了解广播定义及相关原理的可以看下这一篇BroadcastReceiver史上最全面解析 简单地对广播进行分类吧,广播有两个角色,一个是广播发送者,另外一个是广播接收者 广播按照类型分为两种,一种是 ...

  2. POJ 1426 Find The Multiple(数论——中国同余定理)

    题目链接: http://poj.org/problem?id=1426 Description Given a positive integer n, write a program to find ...

  3. linux日志查看命令

    tail tail 命令用于显示文本文件的末尾几行, 对于监控文件日志特别有用 tail example.txt #显示文件 example.txt 的后十行内容: tail -n 20 exampl ...

  4. Content Provider Test过程中遇到的坑

    Content Provider(内容提供器) 一.什么是Content Provider? 直接贴官方文档简介图,笔者太懒了,而且 坑 不在这

  5. (实例篇)LNMP 1.4一键安装包,安装教程

    http://mp.weixin.qq.com/s/l6ijKBwD6tt8jkZytWEIsw https://lnmp.org/download.html 2017-09-11 学习与分享 PHP ...

  6. 在ARC下,assign和weak的区别

    用了很久,却不知道它俩究竟有什么区别,作为一个新手来说,光会用不行,还要懂得原理. 首先说说区别:weak只可以修饰对象                    assign既可以修饰对象也可以修饰基本 ...

  7. ios 继承关系图

  8. Android-第二天(2)

    程序3 SimpleAdapter是扩展性最好的适配器,可以定义各种你想要的布局,而且使用很方便 SimpleAdapter(Context context, List<? extends Ma ...

  9. html静态页面乱码

    1.将文件保存为UTF-8 2.写入以下代码 <!-- 防止中文乱码 --><meta http-equiv="Content-Type" content=&qu ...

  10. python_16_序列化

    如何实现不同编程语言进行交互? json数据,相当于语言中间的沟通桥梁 什么是json数据? imoprt json json.dumps(内容)                    --把内容转换 ...