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

最新文章

  1. POJ1275 Cashier Employment[差分约束系统 || 单纯形法]
  2. JUnit4:Test注解的两个属性:expected和timeout
  3. 【转载】改善数据质量从数据剖析(Data Profiling)开始
  4. editplus快捷键大全之editplus搜索快捷键
  5. boostrap兼容ie及其案例
  6. 实现Action(含Action访问ServletAPI)
  7. Flyer
  8. 使用Promise规定来处理ajax请求的结果
  9. poj 2492 a bug&#39;s life 简单带权并查集
  10. angularjs中的下拉框默认选中
  11. 【转载】java static 关键字的四种用法
  12. 一、查看MVC4还是MVC5
  13. Mybatis_2.基于XML的增删改查
  14. Netty中的基本组件及关系
  15. 8 -- 深入使用Spring -- 5...3 使用@CacheEvict清除缓存
  16. 纯js无缝滚动
  17. C#:匿名类型
  18. 20155328 网络攻防 实验五:MSF基础应用
  19. linux怎么执行jar文件 怎么打可执行的jar包
  20. Java编程的逻辑 (45) - 神奇的堆

热门文章

  1. Cocos2d-x第三方类库不支持arm64的问题解决(64位架构)
  2. vim 插件之 surround.vim
  3. Linux150个常用指令
  4. 如何在Ubuntu14.04中安装mysql
  5. sed的一些tricks
  6. 关于集合类set
  7. SQL Server字符串分割函数
  8. C#结构函数
  9. Thinkphp的 is null 查询条件是什么,以及exp表达式如何使用
  10. MyBatis自动生成代码之generatorConfig配置文件及其详细解读