J - Infinite monkey theorem

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Appoint description: 
System Crawler  (2014-11-09)

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;
}

  

最新文章

  1. php+ajax..转
  2. 基于华清远见STM32f051的 IIC从模式实现方法
  3. 2014-2015 ACM-ICPC, NEERC, Moscow Subregional Contest D. Do it Right!
  4. 移动端H5页面之iphone6的适配(转)
  5. C++中new的解说
  6. 【SpringMVC】SpringMVC系列1之HelloWorld
  7. TI CC2541的GPIO引脚设置.
  8. Apache-Shiro+Zookeeper系统集群安全解决方案之缓存管理
  9. PHP高级特性二之文件处理
  10. Cordova5 -- iOS实战(一)
  11. BZOJ 4009 接水果
  12. hdu2089:不要62(基础数位dp)
  13. js获取来源和当前域名
  14. Git学习笔记1--Git原理简单介绍
  15. 201521123049 《JAVA程序设计》 第6周学习总结
  16. 阿里云 virtual memory exhausted: 无法分配内存
  17. MySQL 数学函数
  18. linux 下搭建php环境
  19. kvm热添加和热迁移
  20. 关于linux 安装libxml2

热门文章

  1. 前端~HTML~CSS~JavaScript~JQuery~Vue
  2. Wormholes---poj3259(最短路 spfa 判断负环 模板)
  3. Magento 2 初探
  4. Ftp服务器配置讲解
  5. python提取相对路径
  6. android 控件加圆角
  7. 006-markdown基础语法
  8. 【开发者笔记】利用ab命令对接口进行压力测试
  9. mysql根据经纬度获取附近的商家
  10. PHP中使用OpenSSL下openssl_verify验证签名案例