hdu 3689 杭州 10 现场 J - Infinite monkey theorem 概率dp kmp 难度:1

J - Infinite monkey theorem
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Appoint description: 

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%
 
感想:幸亏样例给的好,在这个地方不匹配.前面的还能匹配,知道这个就很容易了

思路:首先得到每个串最长能够匹配多长,然后按照转移次数每次都乘上相对的概率即可

 

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n,m;
double p[26];
char buff[1001];
double dp[1200][30];
int c[30][30];
int main(){
    while(scanf("%d%d",&n,&m)==2&&n&&m){
        memset(p,0,sizeof(p));
        for(int i=0;i<n;i++){
            scanf("%s",buff);
            scanf("%lf",&p[(buff[0]-'a')]);
        }
        scanf("%s",buff);
        int len=strlen(buff);
        char s[30];
        for(int i=0;i<len;i++)//暴力处理出最长匹配长度
        {
            for(int j=0;j<26;j++)//在已匹配长度上,最后一位是什么
            {
                s[i]=j+'a';
                int maxn=0;
                for(int k=len-1;k>=0;k--)//暴力求得可以匹配的最长长度
                {
                    int v=k,x=i;
                    while(v>=0&&i>=0&&s[x]==buff[v])
                    {
                        v--;
                        x--;
                    }
                    if(v==-1)
                        maxn=max(k+1,maxn);
                }
                c[i][j]=maxn;
            }
            s[i]=buff[i];//匹配的时候
        }
        for(int i=0;i<=m;i++)//memset
        {
            for(int j=0;j<=len;j++)
                dp[i][j]=0;
        }
        dp[0][0]=1;
        double sum=0;
        for(int i=0;i<m;i++)//只能敲打m次
        {
            for(int j=0;j<len;j++)
            {
                for(int k=0;k<26;k++)
                {
                    dp[i+1][c[j][k]]+=dp[i][j]*p[k];
                }
            }
        }
        for(int i=1;i<=m;i++)
        {
            sum+=dp[i][len];
        }
        printf("%.2f%%\n",sum*100);
    }
    return 0;
}

  

posted @ 2014-11-09 18:34  雪溯  阅读(197)  评论(0编辑  收藏  举报