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. 双击Table表格td变成text修改内容

    //先不多说这里上我的页面 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...

  2. AC日记——宠物收养所 bzoj 1208

    1208 思路: 一棵splay树: 如果来者是宠物且树空,就将其加入树中: 如果树不空,则查找前驱后继,取最优,然后删点: 对人亦然: 注意边界和取模,最后的ans用long long其余用int即 ...

  3. Codeforces 509E(思维)

                                                                                                         ...

  4. getenv, _wgetenv

    Description The C library function char *getenv(const char *name) searches for the environment strin ...

  5. js 日期计算星座 根据生日的月份和日期,一行代码计算星座的js小函数(转)

    本博客根据 开源中国作者清风徐不来 的文章 根据生日的月份和日期,一行代码计算星座的js小函数(转) 原文出自CSDN 无心的专栏 的文章,知识产权归原文作者所有! 点击查看原文:js 日期计算星座

  6. 解决Ubuntu下gedit中文乱码的情况

    windows下简体中文多用GBK编码 (或GB2312, GB18030), 而linux下多用UTF-8编码. 因此,一般来说在微软平台编辑的中文txt不能在ubuntu下直接打开查看,除非在保存 ...

  7. Ubuntu下sudo apt-get install vim 失败的解决办法

    Ubuntu下 执行命令:sudo apt-get install vim 失败 解决办法: 更新一下,命令:sudo apt-get update 再安装即可成功:sudo apt-get inst ...

  8. C#调用C++Dll封装时遇到的一系列问题【转】

      最近帮底层开发的同时用C#重新封装一下dll,也就是用C#类来封装C++Dll里的方法,以供用户使用. 之前也用到过类似的应用,大多数问题都出在类型转换上,但是这次的应用层出不穷,所以在这里总结一 ...

  9. [Android实例] Scroll原理-附ScrollView源码分析 (转载)

    想象一下你拿着放大镜贴很近的看一副巨大的清明上河图, 那放大镜里可以看到的内容是很有限的, 而随着放大镜的上下左右移动,就可以看到不同的内容了 android中手机屏幕就相当于这个放大镜, 而看到的内 ...

  10. C#自定义MessageBox 按钮的Text

    运行效果: 代码: using System; using System.Drawing; using System.Runtime.InteropServices; using System.Tex ...