zoukankan      html  css  js  c++  java
  • hdu 3689 Infinite monkey theorem

    Infinite monkey theorem

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    http://acm.hdu.edu.cn/showproblem.php?pid=3689


    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%
     
    字符串均从0开始
    dp[i][j]表示生成到第i个字符,匹配到第j个字符的概率
    到 表示准备匹配,还没有匹配
    对字符串做kmp
    nxt 表示生成第i个字符为k后,由原匹配位置j转移到新的匹配位置nxt
    dp[i+1][nxt]+=dp[i][j]*p[k]
     
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    double p[30];
    char ch[30],s[1001];
    int f[1001];
    double dp[1011][30];
    int main()
    {
        int n,m,len,j;
        while(scanf("%d%d
    ",&n,&m)!=EOF)
        {
            if(!n) return 0;
            for(int i=1;i<=n;i++) 
             scanf("%c %lf
    ",&ch[i],&p[i]);
            scanf("%s",s);
            len=strlen(s);
            for(int i=1;i<len;i++)
            {
                j=f[i];
                while(j&&s[j]!=s[i]) j=f[j];
                f[i+1]= s[j]==s[i] ? j+1 : 0;
                
            }
            int nxt;
            memset(dp,0,sizeof(dp));
            dp[0][0]=1;
            for(int i=0;i<m;i++) 
             for(int j=0;j<len;j++)
              for(int k=1;k<=n;k++)
              {
                  nxt=j;
                  while(nxt&&s[nxt]!=ch[k]) nxt=f[nxt];
                  if(s[nxt]==ch[k]) nxt++; 
                  dp[i+1][nxt]+=dp[i][j]*p[k];
              }
            double ans=0;
            for(int i=0;i<=m;i++) ans+=dp[i][len];
            printf("%.2lf%%
    ",ans*100);
        }
    }
  • 相关阅读:
    bzoj 1827: [Usaco2010 Mar]gather 奶牛大集会
    bzoj 1690: [Usaco2007 Dec]奶牛的旅行——分数规划+spfa判负环
    poj2728 最小比率生成树——01分数规划
    H265编码网页视频流媒体播放器EasyWasmPlayer.js播放控制台提示Uncaught TypeError排查
    EasyRTSPServer对接海康录像机无法正常预览如何解决?
    如何对EasyRTSPLive进行修改将其支持多通道拉RTSP流推RTMP流功能?
    H265视频流媒体播放器EasyPlayer.js播放HLS视频流无法自动播放原因排查分析
    从“地球漫游计划”看城市,TSINGSEE青犀视频让城市漫游直播更简单
    核污水排入大海?大到全球,小到城市,环境污染应该如何通过视频监控进行监管?
    AI如何让视频更智能?人工智能技术在视频监控中的应用
  • 原文地址:https://www.cnblogs.com/TheRoadToTheGold/p/6953737.html
Copyright © 2011-2022 走看看