[AC自己主动机+可能性dp] hdu 3689 Infinite monkey theorem
意甲冠军:
给n快报,和m频率。
然后进入n字母出现的概率
然后给目标字符串str
然后问m概率倍的目标字符串是敲数量。
思维:
AC自己主动机+可能性dp简单的问题。
首先建立trie图,然后就是状态转移了
dp版本号:
dp三重循环变量次数,节点数,和字母数
代码:
#include"cstdlib"
#include"cstdio"
#include"cstring"
#include"cmath"
#include"queue"
#include"algorithm"
#include"iostream"
#include"map"
#include"string"
using namespace std;
int triecont;
double dp[1234][22];
struct trie
{
int mark,id;
trie *next[27],*fail;
trie()
{
mark=id=0;
memset(next,0,sizeof(next));
fail=NULL;
}
};
trie *root,*node[22];
void init(char *v)
{
trie *p=root;
for(int i=0;v[i];i++)
{
int tep=v[i]-'a';
if(p->next[tep]==NULL)
{
p->next[tep]=new trie();
node[triecont]=p->next[tep];
p->next[tep]->id=triecont++;
}
p=p->next[tep];
}
p->mark++;
}
void getac()
{
queue<trie*>q;
q.push(root);
while(!q.empty())
{
trie *p=q.front();
q.pop();
for(int i=0;i<26;i++)
{
if(p->next[i]==NULL)
{
if(p==root) p->next[i]=root;
else p->next[i]=p->fail->next[i];
}
else
{
if(p==root) p->next[i]->fail=root;
else p->next[i]->fail=p->fail->next[i];
q.push(p->next[i]);
}
}
}
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m),(n+m))
{
double gl[27];
memset(gl,0,sizeof(gl));
memset(node,0,sizeof(node));
while(n--)
{
char x[2];
double y;
scanf("%s%lf",x,&y);
gl[x[0]-'a']=y;
}
char fuck[27];
scanf("%s",fuck);
triecont=0;
root=new trie();
node[triecont]=root;
root->id=triecont++;
init(fuck);
getac();
memset(dp,0,sizeof(dp));
dp[0][0]=1;
for(int i=1;i<=m;i++)
{
for(int j=0;j<triecont-1;j++)
{
for(int k=0;k<26;k++)
{
trie *p=node[j]->next[k];
dp[i][p->id]+=dp[i-1][j]*gl[k];
}
}
}
double ans=0;
for(int i=0;i<=m;i++) ans+=dp[i][triecont-1]; printf("%.2f%%\n",ans*100);
}
return 0;
}
建立可达矩阵版本号:
注意到达目标状态 那么他之后的状态的概率就都是1了
然后用高速幂加速~
#include"cstdlib"
#include"cstdio"
#include"cstring"
#include"cmath"
#include"queue"
#include"algorithm"
#include"iostream"
using namespace std;
int triecont;
struct trie
{
int mark,id;
trie *next[27];
trie *fail;
trie()
{
mark=id=0;
memset(next,0,sizeof(next));
fail=NULL;
}
};
struct matrix
{
double mat[20][20];
};
trie *root;
void init(char *v)
{
trie *p=root;
for(int i=0; v[i]; i++)
{
int tep=v[i]-'a';
if(p->next[tep]==NULL)
{
p->next[tep]=new trie();
p->next[tep]->id=triecont++;
}
p=p->next[tep];
}
p->mark=1;
}
void getac()
{
queue<trie*>q;
q.push(root);
while(!q.empty())
{
trie *p;
p=q.front();
q.pop();
for(int i=0; i<26; i++)
{
if(p->next[i]==NULL)
{
if(p==root) p->next[i]=root;
else p->next[i]=p->fail->next[i];
}
else
{
if(p==root) p->next[i]->fail=root;
else p->next[i]->fail=p->fail->next[i];
q.push(p->next[i]);
}
}
}
}
matrix matmul(matrix a,matrix b,int n)
{
int i,j,k;
matrix c;
memset(c.mat,0,sizeof(c.mat));
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
for(k=0; k<n; k++)
{
c.mat[i][j]+=a.mat[i][k]*b.mat[k][j];
}
}
}
return c;
}
matrix matpow(matrix a,int k,int n)
{
matrix b;
int i;
memset(b.mat,0,sizeof(b.mat));
for(i=0; i<n; i++) b.mat[i][i]=1;
while(k)
{
if(k&1) b=matmul(a,b,n);
a=matmul(a,a,n);
k>>=1;
}
return b;
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m),(n+m))
{
double gl[27];
memset(gl,0,sizeof(gl));
while(n--)
{
char x[2];
double y;
scanf("%s%lf",x,&y);
gl[x[0]-'a']+=y;
}
triecont=0;
root=new trie();
root->id=triecont++;
char x[12];
scanf("%s",x);
init(x);
getac();
queue<trie*>q;
q.push(root);
int used[12];
memset(used,0,sizeof(used));
matrix a,ans;
memset(a.mat,0,sizeof(a.mat));
while(!q.empty())
{
trie *p=q.front();
q.pop();
if(used[p->id]) continue;
used[p->id]=1;
if(p->mark==1) //目标状态 兴许状态都是本身
{
a.mat[p->id][p->id]=1;
continue;
}
for(int i=0;i<26;i++)
{
if(used[p->next[i]->id]==0) q.push(p->next[i]);
a.mat[p->id][p->next[i]->id]+=gl[i];
}
}
/*for(int i=0;i<triecont;i++)
{
for(int j=0;j<triecont;j++) printf("%.2f ",a.mat[i][j]);
puts("");
}*/
ans=matpow(a,m,triecont);
printf("%.2f%%\n",ans.mat[0][triecont-1]*100);
}
return 0;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
[AC自己主动机+可能性dp] hdu 3689 Infinite monkey theorem的更多相关文章
- 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 ...
- HDU 3689 Infinite monkey theorem [KMP DP]
Infinite monkey theorem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- hdu 3689 Infinite monkey theorem
Infinite monkey theorem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...
- [HDU 3689]Infinite monkey theorem (KMP+概率DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3689 黄老师说得对,题目只有做wa了才会有收获,才会有提高. 题意:一个猴子敲键盘,键盘上有n个键,猴 ...
- HDU 3689 Infinite monkey theorem ——(自动机+DP)
这题由于是一个单词,其实直接kmp+dp也无妨.建立自动机当然也是可以的.设dp[i][j]表示匹配到第i个字母的时候,在单词中处于第j个位置的概率,因此最终的答案是dp[0~m][len],m是输入 ...
- ●HDU 3689 Infinite monkey theorem
题链: http://acm.hdu.edu.cn/showproblem.php?pid=3689题解: KMP,概率dp (字符串都从1位置开始) 首先对模式串S建立next数组. 定义dp[i] ...
- ZOJ 3494 BCD Code (AC自己主动机 + 数位DP)
题目链接:BCD Code 解析:n个病毒串.问给定区间上有多少个转换成BCD码后不包括病毒串的数. 很奇妙的题目. . 经典的 AC自己主动机 + 数位DP 的题目. 首先使用AC自己主动机,得到b ...
- POJ 3691 & HDU 2457 DNA repair (AC自己主动机,DP)
http://poj.org/problem?id=3691 http://acm.hdu.edu.cn/showproblem.php?pid=2457 DNA repair Time Limit: ...
- HDU 2825 Wireless Password (AC自己主动机,DP)
pid=2825">http://acm.hdu.edu.cn/showproblem.php? pid=2825 Wireless Password Time Limit: 2000 ...
随机推荐
- 【转】HTML5移动端最新兼容问题解决方案
1.安卓浏览器看背景图片,有些设备会模糊. 用同等比例的图片在PC机上很清楚,但是手机上很模糊,原因是什么呢? 经过研究,是devicePixelRatio作怪,因为手机分辨率太小,如果按照分辨率来显 ...
- 我的前端规范——JavaScript篇
相关文章 简书原文:https://www.jianshu.com/p/5918c283cdc3 我的前端规范——开篇:http://www.cnblogs.com/shcrk/p/9271561.h ...
- Spring Boot+Mybatis+Pagehelper分页
Spring Boot 集成MyBatis和Pagehelper分页插件 mybatis-spring-boot-starter依赖树如下: pom配置 <project xmlns=" ...
- [CSS] Easily Reset Styles With a Single CSS value
There are times where you need to reset a an element’s styles. Instead of overwriting it with even m ...
- ASP.Net WebAPI HttpDelete/PUT方法运行或发布到生产服务器上后出现405(Method Not Allowed)错误的解决办法
原文:ASP.Net WebAPI HttpDelete/PUT方法运行或发布到生产服务器上后出现405(Method Not Allowed)错误的解决办法 本文只是个人的理解和学习记录,如果觉得本 ...
- [Git] How to rename your remote branch
Rename your local foo branch with bar: git branch -m foo bar Remember this will add the new branch w ...
- ExtJs中window用法
1.显示html var htmlTitle = "<div style='width:100%;text-align:center'>"; var fruits = ...
- [转] Valgrind使用
http://www.cnblogs.com/napoleon_liu/articles/2001802.html 调不尽的内存泄漏,用不完的Valgrind Valgrind 安装 1. 到www. ...
- js进阶 10-6 jquery中的属性选择器有哪些
js进阶 10-6 jquery中的属性选择器有哪些 一.总结 一句话总结: 1.第一遍能学会么? 一遍是肯定学不会的,要多学几遍,所以想着怎么加快速度,减少学习的遍数 2.属性选择器是干嘛的? 选择 ...
- 【b304】传染病防治
Time Limit: 1 second Memory Limit: 50 MB [问题背景] 近来,一种新的传染病肆虐全球.蓬莱国也发现了零星感染者,为防止该病在蓬莱国 大范围流行,该国政府决定不惜 ...