"BZOJ1444" [Jsoi2009] Interesting game description
Input
Attention, 0<=p.
Output
Sample Input
Sample Output
HINT
30% of data guaranteed, n≤2. 50% of data guaranteed, n≤5. 100% data guarantee, N, L, m≤10.
Solution: The practice of the subject is really a lot ah, probability DP, expect DP, of course, and the moment by black technology ~
is to run the AC automaton first, make the transfer matrix, and then squared 50 times on the line.
#include <cstdio> #include <cstring> #include <iostream> #include <queue>using namespace std; int n,l,m,tot;double c[30],ans[20];queue<int> q;struct node{int Ch[30],fail,dan;} P[110];char str[20];struct m{double v[110][110]; M () {memset (v,0,sizeof (v));} Double* operator [] (int x) {return v[x];} M operator * (M a) const{m c;for (int i=1;i<=tot;i++) for (int. j=1;j<=tot;j++) for (int k=1;k<=tot;k++) c[i][j]+=v[i ][k]*a[k][j];return c;}}; M x;void Build () {int I,u;q.push (1), while (!q.empty ()) {U=q.front (), Q.pop (), for (i=0;i<m;i++) {if (!p[u].ch[i]) {if (U ==1) P[u].ch[i]=1;elsep[u].ch[i]=p[p[u].fail].ch[i];continue;} Q.push (P[u].ch[i]); if (u==1) {p[p[u].ch[i]].fail=1;continue;} P[p[u].ch[i]].fail=p[p[u].fail].ch[i];}}} int main () {scanf ("%d%d%d", &n,&l,&m), int i,j,u;double a,b;for (i=0;i<m;i++) scanf ("%lf%lf", &a, &B), C[i]=a/b;for (tot=i=1;i<=n;i++) {scanf ("%s", str), U=1;for (j=0;j<l;j++) {if (!p[u].ch[str[j]-' a ']) P[u] . ch[str[j]-' A ']=++tot;u=p[u].ch[str[j]-' a ';}P[u].dan=i;} Build (); for (i=1;i<=tot;i++) {if (P[i].dan) x[i][i]=1;elsefor (j=0;j<m;j++) x[i][p[i].ch[j]]+=c[j];} for (i=1;i<=50;i++) x=x*x;for (i=1;i<=tot;i++) if (P[i].dan) ans[p[i].dan]=x[1][i];for (i=1;i<=n;i++) printf ( "%.2lf\n", Ans[i]); return 0;} /*1 101 101 101 101 101 101 101 101 101 101 10AAAAAAAAAA * *
"BZOJ1444" [Jsoi2009] Interesting game AC automata + probabilistic dp+ matrix multiplication