HUD3689 Infinite monkey theorem
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
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.
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
0.00%
98.54%
动态规划+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的更多相关文章
- HDU 3689 Infinite monkey theorem [KMP DP]
Infinite monkey theorem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- 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
Infinite monkey theorem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...
- 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
题链: http://acm.hdu.edu.cn/showproblem.php?pid=3689题解: KMP,概率dp (字符串都从1位置开始) 首先对模式串S建立next数组. 定义dp[i] ...
- hdu-3689 Infinite monkey theorem 概率dp+kmp
有一只猴子随机敲键盘,给出它可能敲的键以及敲各个键的概率. 输入:n,表示有多少个键,m,表示猴子会敲m次键 n个二元组(字母,数字) 表示键代表的字母及其被敲的概率. 最后一个目标字符串. 问这只猴 ...
- Infinite monkey theorem(hdu 3689)
题意:问随机生成一个长度为m(m<=1000)长度的字符串,出现某个子串s的概率是多少. /* KMP+DP 设f[i][j]表示A生成到第i位,此时B串匹配到第j位的概率. 转移方程为f[i+ ...
- HDU3689 Infinite monkey theorem 无限猴子(字符串DP+KMP)
题目描述: 大概的意思就是根据无限猴子定理,无限只猴子坐在打字机旁瞎敲,总有一个能敲出莎士比亚文集.现在给你一个打字机和一只猴子,打字机的每个按钮(共n个)上的字母及猴子按下这个按钮的概率已知,而且猴 ...
随机推荐
- Codeforces Gym101063 J.The Keys (2016 USP-ICMC)
J.The Keys Out of all science labs constructed by the GEMA mission on Mars, the DSL - Dangerous Spec ...
- Codeforces Round #467 (Div. 2) B. Vile Grasshoppers[求去掉2-y中所有2-p的数的倍数后剩下的最大值]
B. Vile Grasshoppers time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Algorithm | hash
A basic requirement is that the function should provide a uniform distribution of hash values. A non ...
- CentOS 7下安装配置FTP
安装vsftpd yum install -y vsftpd 编辑ftp配置文件 vi /etc/vsftpd/vsftpd.conf anonymous_enable=NO #anonymous_e ...
- spring配置文件中配置sessionFactory失败
配置失败主要原因有两个: <bean id="studentDaoImp" class="com.gxwuz.maven.dao.StudentDaoImp&quo ...
- python中mp3转wav(Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work")
1.下载pydub:pip install pydub 2.下载与操作系统一致的ffmpeg:http://ffmpeg.org/download.html 3.解压后将下载的ffmpeg下的bin目 ...
- 2016集训测试赛(二十)Problem A: Y队列
Solution 考虑给定一个\(n\), 如何求\(1\)到\(n\)的正整数中有多少在队列中. 不难注意到我们只需要处理质数次方的情况即可, 因为合数次方会被其因数处理到. 同时我们考虑到可能存在 ...
- Spring MVC通过Pageable对象和PageableDefault注解获取分页信息(MongoDB通过Pageable来操作分页)
说明:Pageable同时也能用于操作MongoDB的分页. PageableSpring Data库中定义的一个接口,该接口是所有分页相关信息的一个抽象,通过该接口,我们可以得到和分页相关所有信息( ...
- 跳转移动端js代码
<script language="JavaScript"> $(function(){ var MobileUA = (function() { var ua = n ...
- 受检查异常要求try catch,new对象时,就会在堆中创建内存空间,创建的空间包括各个成员变量类型所占用的内存空间
,new对象时,就会在堆中创建内存空间,创建的空间包括各个成员变量类型所占用的内存空间