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. SDOI2017round1酱油记day0

    嗯... 现在是21:12...准备睡了. 睡前写下day0一天如何过的: 早上5点起床到教室早自习,迷迷糊糊的宣誓,背东西,英语听写: 都停课了为什么还要上早自习! 我!想!去!机!房! OI才是我 ...

  2. Ticket Lock, CLH Lock, MCS Lock

    如果不用OS提供的mutex,我们该如何实现互斥锁?(不考虑重入的情况) 1. naive lock 最简单的想法是,搞一个volatile类型的共享变量flag,值可以是flase(无锁)或者tru ...

  3. my.ini配置详解

    Mysql my.ini 配置文件详解 #BEGIN CONFIG INFO #DESCR: 4GB RAM, 只使用InnoDB, ACID, 少量的连接, 队列负载大 #TYPE: SYSTEM ...

  4. (入门SpringBoot)SpringBoot发送邮件(十一)

    SpringBoot配置邮件服务: 1.引入jar <!-- 邮件 --> <dependency>    <groupId>org.springframework ...

  5. OnClick五种事件处理

    (一)内部类 1,布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns: ...

  6. 适配iOS7之—UITableView和UISearchBar

    iOS7中,如果用UITableViewStyleGrouped的话,里面的cell会比原来的拉长了,这样做应该是为了统一和UITableViewStylePlain风格时cell的大小一致,所以改用 ...

  7. Xocde 自动注释插件

    github 地址 https://github.com/onevcat/VVDocumenter-Xcode   可以对xcode方法进行类似java那样的自动注释 源码下载下后编译运行一次  xo ...

  8. xamarin.android 消息推送功能--极光推送

    最近在使用xamarin.android的消息推送功能,官方使用的例子是FCM方式,按照官方文档,使用FQ软件是可以成功的,但是在国内由于众所周知的原因,在国内服务并不能使用,于是查找国内各自推送平台 ...

  9. 转: RPC框架 远程对象服务引擎Hprose

    http://www.cnblogs.com/chenxizhang/archive/2010/07/18/1780258.html

  10. Mockito 如何 mock 返回值为 void 的方法

    转载:https://unmi.cc/mockito-how-to-mock-void-method/#more-7748 最初接触 Mockito 还思考并尝试过如何用它来 mock 返回值为 vo ...