【hdu 3689】Infinite monkey theorem(概率dp+kmp)

来源:互联网 发布:帝国cms 附件绑定域名 编辑:程序博客网 时间:2024/05/22 02:30

Infinite monkey theorem

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


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 10w 0.25o 0.25r 0.25d 0.25word2 10a 1.0b 0.0abc2 100a 0.312345b 0.687655abab0 0
 

Sample Output
2.73%0.00%98.54%
 

Source
2010 Asia Hangzhou Regional Contest
 
[题意][给定一些字符的出现概率,求字串s在长为m的字串中出现的概率]
【题解】【概率dp+kmp】
【在做本题前,请先阅读:http://www.matrix67.com/blog/archives/366 ,里面所讲的基本就是这道题的主要思想】
【f[i][j]表示长为m的串第i位匹配到串s中的第j位的概率;由于要求匹配,为了转移方便,需要先求出串s的失配】
【用kmp求完失配后,直接dp即可】

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;char s[1010],c[30];int nxt[1010],n,m,len;double g[1010],f[1010][1010],ans;inline void kmp(){nxt[1]=0;int j=0;for(int i=2;i<=len;++i) { while(j&&s[j+1]!=s[i]) j=nxt[j]; if(s[j+1]==s[i]) j++; nxt[i]=j; }}int main(){int i,j;while(scanf("%d%d",&n,&m)==2) { if(!m&&!n) return 0; memset(f,0,sizeof(f)); getchar(); for(i=1;i<=n;++i) scanf("%c%lf\n",&c[i],&g[i]);scanf("%s",s+1);len=strlen(s+1);kmp(); f[0][0]=1;for(i=0;i<m;++i) for(j=0;j<len;++j)  for(int k=1;k<=n;++k)   {   int tt=j;   while(tt&&s[tt+1]!=c[k]) tt=nxt[tt];   if(s[tt+1]==c[k]) tt++;   f[i+1][tt]+=f[i][j]*g[k];   }ans=0;for(i=0;i<=m;++i) ans+=f[i][len];printf("%.2lf%%\n",ans*100); }return 0; } 


0 0
原创粉丝点击