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[];
char ch[],s[];
int f[];
double dp[][];
int main()
{
int n,m,len,j;
while(scanf("%d%d\n",&n,&m)!=EOF)
{
if(!n) return ;
for(int i=;i<=n;i++)
scanf("%c %lf\n",&ch[i],&p[i]);
scanf("%s",s);
len=strlen(s);
for(int i=;i<len;i++)
{
j=f[i];
while(j&&s[j]!=s[i]) j=f[j];
f[i+]= s[j]==s[i] ? j+ : ; }
int nxt;
memset(dp,,sizeof(dp));
dp[][]=;
for(int i=;i<m;i++)
for(int j=;j<len;j++)
for(int k=;k<=n;k++)
{
nxt=j;
while(nxt&&s[nxt]!=ch[k]) nxt=f[nxt];
if(s[nxt]==ch[k]) nxt++;
dp[i+][nxt]+=dp[i][j]*p[k];
}
double ans=;
for(int i=;i<=m;i++) ans+=dp[i][len];
printf("%.2lf%%\n",ans*);
}
}

hdu 3689 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 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 ...

  3. [HDU 3689]Infinite monkey theorem (KMP+概率DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3689 黄老师说得对,题目只有做wa了才会有收获,才会有提高. 题意:一个猴子敲键盘,键盘上有n个键,猴 ...

  4. ●HDU 3689 Infinite monkey theorem

    题链: http://acm.hdu.edu.cn/showproblem.php?pid=3689题解: KMP,概率dp (字符串都从1位置开始) 首先对模式串S建立next数组. 定义dp[i] ...

  5. [AC自己主动机+可能性dp] hdu 3689 Infinite monkey theorem

    意甲冠军: 给n快报,和m频率. 然后进入n字母出现的概率 然后给目标字符串str 然后问m概率倍的目标字符串是敲数量. 思维: AC自己主动机+可能性dp简单的问题. 首先建立trie图,然后就是状 ...

  6. HDU 3689 Infinite monkey theorem ——(自动机+DP)

    这题由于是一个单词,其实直接kmp+dp也无妨.建立自动机当然也是可以的.设dp[i][j]表示匹配到第i个字母的时候,在单词中处于第j个位置的概率,因此最终的答案是dp[0~m][len],m是输入 ...

  7. 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 ...

  8. HUD3689 Infinite monkey theorem

    Infinite monkey theorem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/ ...

  9. Infinite monkey theorem(hdu 3689)

    题意:问随机生成一个长度为m(m<=1000)长度的字符串,出现某个子串s的概率是多少. /* KMP+DP 设f[i][j]表示A生成到第i位,此时B串匹配到第j位的概率. 转移方程为f[i+ ...

随机推荐

  1. hive创建外部表

    Create [EXTERNAL] TABLE [IF NOT EXISTS] table_name [(col_name data_type [COMMENT col_comment], ...)] ...

  2. Python高级编程-多线程

    (一)进程线程概述: 很多同学都听说过,现代操作系统比如Mac OS X,UNIX,Linux,Windows等,都是支持“多任务”的操作系统. 什么叫“多任务”呢?简单地说,就是操作系统可以同时运行 ...

  3. HDU 3262/POJ 3829 Seat taking up is tough(模拟+搜索)(2009 Asia Ningbo Regional)

    Description Students often have problems taking up seats. When two students want the same seat, a qu ...

  4. iOS-根据两个经纬度计算相距距离

    CLLocation *orig=[[[CLLocation alloc] initWithLatitude:[mainDelegate.latitude_self doubleValue] long ...

  5. Vim新手节省时间的10多个小技巧

    Vim新手节省时间的10多个小技巧 Vim 是很多开发者的首选编辑器,通过设置正确的命令和快捷方式,它可以帮你更快的完成工作.这篇文章我们为 Vim 新手提供一些快捷键等方面的小技巧,帮你提升工作效率 ...

  6. centos设置时间同步

    1.安装ntpdate #yum install ntpdate   2. #cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime #ntpdate ...

  7. 【C/C++语法外功】类的静态成员理解

    例1  孙鑫視頻學習  Oct.27th 2009  Skyseraph 例子1.0 #include "iostream" class Point { public: void ...

  8. linux 安装 bitnamid-redmine

    Unix 和 Linux 安装 Perl Unix/Linux 系统上 Perl 安装步骤如下: 通过浏览器打开 http://www.perl.org/get.html. 下载适用于 Unix/Li ...

  9. js滚动大于多少菜单就开始固定

    //导航置顶 $(window).scroll(function () { var pos = $(window).scrollTop(); ) { $("#menu").addC ...

  10. 【bzoj3036】绿豆蛙的归宿 期望dp

    题目描述 随着新版百度空间的下线,Blog宠物绿豆蛙完成了它的使命,去寻找它新的归宿. 给出一个有向无环的连通图,起点为1终点为N,每条边都有一个长度.绿豆蛙从起点出发,走向终点.到达每一个顶点时,如 ...