Description

Input

注意 是0<=P, n , l, m≤ 10.

Output

Sample Input

input 1
3 2 2
1 2
1 2
AB
BA
AA
input 2
3 4 2
1 2
1 2
AABA
ABAA
BAAA

Sample Output

output 1
0.25
0.50
0.25

output 2
0.31
0.33
0.37

解题思路:

可以理解为Trie上倍增佛洛依德做矩乘累和。

代码:

 #include<cstdio>
#include<cstring>
#include<algorithm>
struct trnt{
int ch[];
int fl;
bool fin;
}tr[];
class queue{
public:
queue(void){h=,t=;return ;}
int nxt(int x){if(x+>=)return ;return x+;}
void push(int x){t=nxt(t);line[t]=x;return ;}
void pop(void){h=nxt(h);return ;}
bool empty(void){return nxt(t)==h;}
int front(void){return line[h];}
private:
int h,t,line[];
}Q;
int siz;
int n,m,l;
int ansp[];
char tmp[];
double lst[];
double ans[];
double martix[][];
double temp[][];
double pre[];
void Insert(char *a,int &plc)
{
int root=;
int len=l;
for(int i=;i<=len;i++)
{
int c=a[i]-'A';
if(!tr[root].ch[c])
tr[root].ch[c]=++siz;
root=tr[root].ch[c];
}
tr[root].fin=true;
plc=root;
return ;
}
void Build(void)
{
int root=;
for(int i=;i<m;i++)
if(tr[root].ch[i])
Q.push(tr[root].ch[i]);
while(!Q.empty())
{
root=Q.front();
Q.pop();
for(int i=;i<m;i++)
{
if(tr[root].ch[i])
{
tr[tr[root].ch[i]].fl=tr[tr[root].fl].ch[i];
Q.push(tr[root].ch[i]);
}else
tr[root].ch[i]=tr[tr[root].fl].ch[i];
}
}
return ;
}
int main()
{
scanf("%d%d%d",&n,&l,&m);
for(int i=;i<m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
pre[i]=(double)(a)/(double)(b);
}
for(int i=;i<=n;i++)
{
scanf("%s",tmp+);
Insert(tmp,ansp[i]);
}
Build();
for(int i=;i<=siz;i++)
{
if(tr[i].fin)
{
martix[i][i]=1.00;
continue;
}
for(int j=;j<m;j++)
martix[i][tr[i].ch[j]]+=pre[j]; }
int t=;
/*for(int i=0;i<=siz;i++)
{
for(int j=0;j<=siz;j++)
printf("%.2lf ",martix[i][j]);
puts("");
}*/
while(t--)
{
for(int i=;i<=siz;i++)
{
for(int j=;j<=siz;j++)
{
temp[i][j]=;
for(int k=;k<=siz;k++)
{
temp[i][j]+=martix[i][k]*martix[k][j];
}
}
}
for(int i=;i<=siz;i++)
for(int j=;j<=siz;j++)
martix[i][j]=temp[i][j];
}
for(int i=;i<=n;i++)
printf("%.2lf\n",martix[][ansp[i]]);
return ;
}

BZOJ1444: [Jsoi2009]有趣的游戏(Trie图,矩乘)的更多相关文章

  1. BZOJ 1444 [JSOI2009]有趣的游戏 (Trie图/AC自动机+矩阵求逆)

    题目大意:给你$N$个长度相等且互不相同的模式串,现在有一个字符串生成器会不断生成字符,其中每个字符出现的概率是$p_{i}/q_{i}$,当生成器生成的字符串包含了某个模式串,则拥有该模式串的玩家胜 ...

  2. BZOJ1444[Jsoi2009]有趣的游戏——AC自动机+概率DP+矩阵乘法

    题目描述 输入 注意 是0<=P, n , l, m≤ 10. 输出 样例输入 input 1 3 2 2 1 2 1 2 AB BA AA input 2 3 4 2 1 2 1 2 AABA ...

  3. BZOJ1444:[JSOI2009]有趣的游戏(AC自动机,矩阵乘法)

    Description Input 注意 是0<=P, n , l, m≤ 10. Output Sample Input input 1 3 2 2 1 2 1 2 AB BA AA inpu ...

  4. [日常摸鱼]bzoj1444 [JSOI2009]有趣的游戏——AC自动机+矩阵

    今天学校跳蚤市场摆摊聚众吸毒打call,东西卖了一百多好开心_(:з」∠)_ (然后大家中午就去吃了一顿好的x) 下午听演讲然后现在来填坑orz(其实是昨晚的坑) 题目:bzoj1444 先用字符串构 ...

  5. BZOJ1444 : [Jsoi2009]有趣的游戏

    建立AC自动机,并求出转移矩阵. 再用$\sum E(终止节点)=1$去替换第一个方程,高斯消元即可. 时间复杂度$O(n^3l^3)$. 注意精度问题,要特判0.00的情况. #include< ...

  6. bzoj1444[Jsoi2009]有趣的游戏[AC自动机]

    题面 bzoj 我要向师父学习善待每一只数据结构 考虑成环,那么高斯消元 然鹅这道题太小了 所以直接转移矩阵自乘就好啦 终点不向外连边 有一条向自己的,概率为一的自环来作为结尾 对于其他店 若有边\( ...

  7. 【BZOJ1444】[JSOI2009]有趣的游戏(高斯消元,AC自动机)

    [BZOJ1444][JSOI2009]有趣的游戏(高斯消元,AC自动机) 题面 BZOJ 题解 先把\(AC\)自动机构建出来,最好构成\(Trie\)图.然后这样子显然是在一个有向图中有一堆概率的 ...

  8. 【BZOJ1444】[Jsoi2009]有趣的游戏 AC自动机+概率DP+矩阵乘法

    [BZOJ1444][Jsoi2009]有趣的游戏 Description Input 注意 是0<=P Output Sample Input Sample Output HINT  30%的 ...

  9. 【bzoj1444】[Jsoi2009]有趣的游戏

    1444: [Jsoi2009]有趣的游戏 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1007  Solved: 334[Submit][Statu ...

随机推荐

  1. shu_1171 十-&gt;二进制转换(输入输出控制)

    cid=1079&pid=19">http://202.121.199.212/JudgeOnline/problem.php?cid=1079&pid=19 分析:主 ...

  2. POJ2485 Highways 【MST】

    Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22842   Accepted: 10525 Descri ...

  3. 异常:error 0152: No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'

    error 0152: No Entity Framework provider found for the ADO.NET provider with invariant name 'System. ...

  4. 在Windows下如何创建指定的虚拟环境

    前几天给大家分享了如何在默认的情况下创建虚拟环境,没来得及上车的伙伴,可以戳这篇文章:在Windows下如何创建虚拟环境(默认情况下).今天小编给大家分享一下,如何创建的指定的Python环境. 创建 ...

  5. perl编程问题

    一.Hash类型 1.hash遍历输出:如果hash遍历输出的时候不是按key则会按数组输出. my %hash=(); ${hash}{"a"}="1"; $ ...

  6. Linux学习总结(1)——Linux命令大全完整版

    Linux命令大全完整版 目    录I 1. linux系统管理命令1 adduser1 chfn(change finger information)1 chsh(change shell)1 d ...

  7. 洛谷 P3585 [POI2015]PIE

    P3585 [POI2015]PIE 题目描述 一张n*m的方格纸,有些格子需要印成黑色,剩下的格子需要保留白色.你有一个a*b的印章,有些格子是凸起(会沾上墨水)的.你需要判断能否用这个印章印出纸上 ...

  8. 3D图形处理库

    转自 3D图形处理库 高性能软件光栅化渲染器 OpenSWR OpenSWR —— 用于OpenGL的高性能,高度可扩展的软件光栅化渲染器 OpenSWR的目的是提供一个高性能,高度可扩展的OpenG ...

  9. OpenCASCADE Job - 武汉中南

    中南设计集团(武汉)工程技术研究院有限公司是中南工程咨询设计集团有限公司(以下简称“中南设计集团”)打造的工程技术研发和科研创新平台,为中南设计集团旗下全资子公司,于2018年2月成立.公司业务范围涵 ...

  10. 62.nodejs中的ejs模板学习

    转自:https://blog.csdn.net/dongmelon/article/details/72403913 02.ejs.这是被include的文件 <script> cons ...