意甲冠军:

给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的更多相关文章

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

  2. HDU 3689 Infinite monkey theorem [KMP DP]

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

  3. hdu 3689 Infinite monkey theorem

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

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

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

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

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

  6. ●HDU 3689 Infinite monkey theorem

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

  7. ZOJ 3494 BCD Code (AC自己主动机 + 数位DP)

    题目链接:BCD Code 解析:n个病毒串.问给定区间上有多少个转换成BCD码后不包括病毒串的数. 很奇妙的题目. . 经典的 AC自己主动机 + 数位DP 的题目. 首先使用AC自己主动机,得到b ...

  8. POJ 3691 &amp; 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: ...

  9. HDU 2825 Wireless Password (AC自己主动机,DP)

    pid=2825">http://acm.hdu.edu.cn/showproblem.php? pid=2825 Wireless Password Time Limit: 2000 ...

随机推荐

  1. x265探索与研究(四):怎样编码视频?

    怎样编码视频?           本文介绍x265中的两种编码视频方法,一是採用命令行的方法.二是GUI的方式.   1.命令行的方法   (1).第一种方式生成*.265格式的视频文件       ...

  2. [HTML] Creating visual skip links in HTML and CSS

    Skip links are an extremely helpful navigation pattern for keyboard and screen reader users, since t ...

  3. Unity3d优化包的大小

    http://wenku.baidu.com/link?url=MEUtNP6k1W7gXK2LcHdKXGqwoTD4HZDsBpsu9iFYjuL3WCIXgl2-rBHhBWP_zo5Xm4Yx ...

  4. MySQL key分区(五)

    具体描写叙述总结请看MySQL分区(一) 样例:该样例为本人个人学习总结分享->具体说明-->有问题欢迎前来交流

  5. 浏览器对象模型(BOM)是什么?(体系结构+知识详解)(图片:结构)

    浏览器对象模型(BOM)是什么?(体系结构+知识详解)(图片:结构) 一.总结 1.BOM操作所有和浏览器相关的东西:网页文档dom,历史记录,浏览器屏幕,浏览器信息,文档的地址url,页面的框架集. ...

  6. Color the ball(杭电1556)

    Color the ball Time Limit : 9000/3000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Tota ...

  7. JDBC连接数据库步骤及Class.forName()(转)

    JDBC连接数据库 JDBC是Sun公司制定的一个可以用Java语言连接数据库的技术. 一.JDBC基础知识 JDBC(Java DataBase Connectivity,java数据库连接)是一种 ...

  8. 2014-07-20 体验到的不是北漂easy

    北京首出租天,房子很潮,这房子我住了一个多月,我希望我真的不会活得很长,世界上只有一个真正的租房,只有明确的家是最好的. 550每月,不包括水电费.我不知道该怎么形容,房间里闪耀的太阳.一个窗口,一扇 ...

  9. Erlang游戏开发-协议

    Erlang游戏开发-协议 选择什么协议? 协议包含通讯协议和数据格式. 通讯协议 通讯协议目前常用的是:HTTP 和TCP .其有各自的特点根据游戏的特点而进行选择. HTTP HTTP比较成熟,使 ...

  10. Word Break II -- leetcode

    Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...