Infinite monkey theorem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1703    Accepted Submission(s): 883

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.
 
Sample Input
4 10
w 0.25
o 0.25
r 0.25
d 0.25
word
2 10
a 1.0
b 0.0
abc
2 100
a 0.312345
b 0.687655
abab
0 0
 
Sample Output
2.73%
0.00%
98.54%
 
Source
 
Recommend
lcy&zhengfeng   |   We have carefully selected several similar problems for you:  3682 3683 3685 3686 3687 
 
题意:
  字符集中有cn个字符(最多26个),给出每个字符的出现概率(它们的和保证为1)
  再给出一个子串B
  求:任给一个长度为N的字符串A(只能包含字符集中的cn种字符),使得B是A的子串的概率。
  N<=1000
分析:

  动态规划+KMP
  想象一边随机生成字符串A,一边用KMP匹配字符串B的过程
  f[i][j]表示随机生成到第i位,此时B串匹配到第j位的概率
  枚举下一位生成字符c,设其生成概率为pc
  假设下一位填c,计算出KMP匹配指针j应该移动到now
  f[i+1][now] += f[i][j]*pc
  已经匹配到第m位的状态不再进行转移
  ans = ∑f[i][m]

#include<cstdio>
#include<cstring>
using namespace std;
const int N=;
int cn,n,m,fail[N];
char B[N],key[];
double p[],f[N][N];
void get_fail(){
int p=;fail[]=;
for(int i=;i<=m;i++){
while(p>&&B[i]!=B[p+]) p=fail[p];
if(B[i]==B[p+]) p++;
fail[i]=p;
}
}
void dp(){
memset(f,,sizeof f);
f[][]=;
for(int i=;i<n;i++){
for(int j=;j<m;j++){
for(int k=;k<=cn;k++){
int now=j;
while(now>&&key[k]!=B[now+]) now=fail[now];
if(key[k]==B[now+]) now++;
f[i+][now]+=f[i][j]*p[k];
}
}
}
double ans=;
for(int i=;i<=n;i++) ans+=f[i][m];
ans*=100.0;
printf("%.2lf%%\n",ans);
}
int main(){
char s[];
while(scanf("%d%d",&cn,&n)==){
if(!cn&&!n) break;
for(int i=;i<=cn;i++){
scanf("%s %lf",s,&p[i]);
key[i]=s[];
}
scanf("%s",B+);
m=strlen(B+);
get_fail();
dp();
}
return ;
}

HUD3689 Infinite monkey theorem的更多相关文章

  1. HDU 3689 Infinite monkey theorem [KMP DP]

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

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

  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+trie+自动机)(2010 Asia Hangzhou Regional Contest)

    Description Could you imaging a monkey writing computer programs? Surely monkeys are smart among ani ...

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

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

  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 Infinite monkey theorem 概率dp+kmp

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

  8. Infinite monkey theorem(hdu 3689)

    题意:问随机生成一个长度为m(m<=1000)长度的字符串,出现某个子串s的概率是多少. /* KMP+DP 设f[i][j]表示A生成到第i位,此时B串匹配到第j位的概率. 转移方程为f[i+ ...

  9. HDU3689 Infinite monkey theorem 无限猴子(字符串DP+KMP)

    题目描述: 大概的意思就是根据无限猴子定理,无限只猴子坐在打字机旁瞎敲,总有一个能敲出莎士比亚文集.现在给你一个打字机和一只猴子,打字机的每个按钮(共n个)上的字母及猴子按下这个按钮的概率已知,而且猴 ...

随机推荐

  1. 【转载】51CTO-Android设置模拟器屏幕大小

    在Eclipse Android中设置模拟器屏幕大小是本文要介绍的内容,主要是来了解并学习Eclipse Android中模拟器的设置,具体关于Eclipse Android内容的详解来看本文.   ...

  2. AndroidStudio不重新运行,Debug调试已有进程

    们在使用AndroidStudio进行编写Android应用程序的时候,经常需要对抛出的问题进行断点调试跟踪,如果不知道怎样直接调试已经运行的进程.那么调试的效率会非常低下!下面我就来教大家如何快速调 ...

  3. 怎样在action中获得值栈ValueStack中的值

    1,实现RequestAware接口 //模拟对象    User model=new User();    user.setName=“lisi”;2,ValueStack value=(Value ...

  4. Nowcoder Girl 参考题解【待写】

    [官方题解]:https://www.nowcoder.com/discuss/65411?toCommentId=1134823 [题目链接]:https://www.nowcoder.com/te ...

  5. IM即时通讯群组头像拼接.net core 解决方案

    一.需求概述 多人聊天(群组,讨论组,聊天室,以下统称: “群组” )生成一个拼接头像,需要把最先加入群组的几个人(最多4个人,以下简称:头部用户,A.B.C.D)的头像拼凑成在一起. 群组创建后,A ...

  6. Docker 容器网络

    默认网络 当安装docker时,它会自动创建3个网络.可以使用docker network ls 来查看.   zane@zane-V:~$ docker network ls NETWORK ID ...

  7. innodb事务锁

    计算机程序锁   控制对共享资源进行并发访问 保护数据的完整性和一致性   lock  主要是事务,数据库逻辑内容,事务过程 latch/mutex 内存底层锁:   更新丢失 原因: B的更改还没有 ...

  8. MongoDB下载安装測试及使用

    1.下载安装 64位:mongodb-win32-x86_64-enterprise-windows-64-2.6.4-signed.msi 余数为1的 db.collection.find({ &q ...

  9. Jenkins安装war版本

    Jenkins的war包安装很简单: 下载jenkins的war包地址:https://jenkins.io/download/ 选择对应的版本 然后放入tomcat启动就好,其他根据提示来就好,比较 ...

  10. 使用Unitils测试DAO层

    Spring 的测试框架为我们提供一个强大的测试环境,解决日常单元测试中遇到的大部分测试难题:如运行多个测试用例和测试方法时,Spring上下文只需创建一次:数据库现场不受破坏:方便手工指定Sprin ...