Topic: given n pattern strings, define the damage of a string to the maximum number of pattern strings included in the division of all substrings, to find the damage expectation of a string of length Len
Small five prpr, PRPR, big Miss PRPR
First, an AC automaton is established F[I][J] indicates the damage expectation of the J-node on the AC automaton for a string of length I
If you want to go to a node that is a dangerous node or a fail pointer pointing at a dangerous node, ans++, then go back to the root node
This constructs a matrix to do a quick power = = Do it All--no, don't scold me--
But there's no answer when we run out--so we need a little makeover--
Create a new node if a node is returned to the root because of danger, and a side is attached to the node at the same time
The only edge edge of the new node is 1 points to itself--so that the adjacency matrix squared len, the resulting matrix of the [Root][new_node] is the answer--
Equivalent to the AC automaton as a graph to find the root of the new node to go exactly len step expectations
Good question--pay attention to open long double otherwise card essence--even though it is always stronger than those with double to card long double precision--
Most of the night I write is what I do not understand--not understand the place to see the code it is hoped that the writing can also clear point ... Bar ...
#include <cstdio> #include <cstring> #include <iomanip> #include <iostream> #include < Algorithm> #define M 110using namespace Std;int n,len,alphabet;char s[m];namespace aho_corasick_automaton{struct Trie{trie *son[26],*fail;bool Ed;} Mempool[m],*c=mempool,*root;void Insert (trie* &p,char *s) {if (!p) p=c++;if (!*s) {P->ed=1;return;} Insert (p->son[(*s)-' a '],s+1);} void Build_tree () {static Trie *q[m];int i,r=0,h=0;for (i=0;i<26;i++) if (Root->son[i]) (Q[++r]=root->son[i])- >fail=root;elseroot->son[i]=root;while (r!=h) {Trie *p=q[++h];for (i=0;i<26;i++) if (P->son[i]) {p-> Son[i]->fail=p->fail->son[i];p->son[i]->ed|=p->fail->son[i]->ed;q[++r]=p->son[i];} Elsep->son[i]=p->fail->son[i];}}} namespace Matrix_multiplication{int size;struct Matrix{long double memory[m][m]; Matrix () {memset (memory,0,sizeof memory);} Long double* operator [] (int x) {return memory[x];} friend void operator *= (Matrix &x,matrix &y) {int I,J,k; Matrix z;for (i=0;i<=size;i++) for (j=0;j<=size;j++) for (k=0;k<=size;k++) z[i][j]+=x[i][k]*y[k][j];x=z;} F Matrix Quick_power (Matrix X,int y) {matrix re;for (int i=0;i<=size;i++) re[i][i]=1;while (y) {if (y&1) re*=x;x*=x;y >>=1;} return re;}} int main () {using namespace aho_corasick_automaton;using namespace Matrix_multiplication;int i,j;cin>>n>> Len>>alphabet;for (i=1;i<=n;i++) {scanf ("%s", s+1); Insert (root,s+1);} Build_tree (); Size=c-mempool;long double Temp=1.0/alphabet;for (j=0;j<size;j++) {for (i=0;i<alphabet;i++) {int k ; if (mempool[j].son[i]->ed) {f[j][0]+=temp;f[j][size]+=temp;} Elsef[j][mempool[j].son[i]-mempool]+=temp;}} F[size][size]=1; Matrix Ans=quick_power (F,len); cout<<fixed<<setprecision <<ans[0][size];return 0;}
Bzoj 2553 BeiJing2011 Taboo ac automata + matrix multiplication