​http://www.elijahqi.win/archives/2894​

bzoj1444 [Jsoi2009]有趣的游戏_git


bzoj1444 [Jsoi2009]有趣的游戏_#include_02


bzoj1444 [Jsoi2009]有趣的游戏_git_03


bzoj1444 [Jsoi2009]有趣的游戏_hive_04


bzoj1444 [Jsoi2009]有趣的游戏_hive_05

#include<queue>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>
#define N 110
using namespace std;
inline int read(){
int x=0;char ch=getchar();
while(!isdigit(ch)) ch=getchar();
while(isdigit(ch)) x=x*10+ch-'0',ch=getchar();
return x;
}
bool ed[N];int end[N],trans[N][26],cnt=1,fail[N];
inline void insert1(char s[],int id){
int len=strlen(s+1);int p=1;
for (int i=1,nxt;i<=len;++i){
if (!trans[p][s[i]-'A']) trans[p][s[i]-'A']=nxt=++cnt;
else nxt=trans[p][s[i]-'A'];p=nxt;
}end[id]=p;ed[p]=1;
}
inline void buildAC(){
queue<int>q;q.push(1);for (int i=0;i<26;++i) trans[0][i]=1;
while(!q.empty()){
int x=q.front();q.pop();
for (int i=0;i<26;++i){
int &y=trans[x][i];
if (y) fail[y]=trans[fail[x]][i],q.push(y);
else {y=trans[fail[x]][i];continue;}ed[y]|=ed[trans[fail[x]][i]];
}
}
}
struct matrix{double f[N][N];}ans,ans1;
inline matrix multiply(const matrix &a,const matrix &b){
matrix c;memset(c.f,0,sizeof(c.f));
for (int i=1;i<=cnt;++i)
for (int k=1;k<=cnt;++k)
for (int j=1;j<=cnt;++j)
c.f[i][j]+=a.f[i][k]*b.f[k][j];
return c;
}
int n,l,m,pp,q;double p[N];char s[N];
int main(){
freopen("bzoj1444.in","r",stdin);
n=read();l=read();m=read();
for (int i=0;i<m;++i) pp=read(),q=read(),p[i]=(double)pp/q;
for (int i=1;i<=n;++i) scanf("%s",s+1),insert1(s,i);buildAC();
for (int i=1;i<=cnt;++i){
if(ed[i]) {ans.f[i][i]=1;continue;}
for (int j=0;j<m;++j) {
int y=trans[i][j];ans.f[i][y]+=p[j];
}
}//for (int i=1;i<=cnt;++i) ans1.f[i][i]=1;
//for (int t=50;t;ans=multiply(ans,ans),t>>=1) if(t&1) ans1=multiply(ans1,ans);
for (int i=1;i<=50;++i) ans=multiply(ans,ans);
for (int i=1;i<=n;++i) printf("%.2f\n",ans.f[1][end[i]]);
return 0;
}