/****
AC自动机 + 高斯消元
AC自动机判断当前状态i通过j能到达的所有状态
E(i)表示当前状态为i,到最终状态的期望
E(i) = Sum(E(next[i][j])+1)/n;
最终结果就是E[0]
******/
#include
#include
#include
using namespace std;
#define maxn 500010
struct Trie{
int next[maxn][26],fail[maxn],end[maxn];
int root,L;
int newNode(){
for(int i=0;i<26;i++) next[L][i] = -1;
end[L++] = 0;
return L-1;
}
void init(){
L = 0;
root = newNode();
}
void insert(char str[]){
//printf("insert:%s\n",str);
int len = strlen(str);
int now = root;
for(int i=0;i
que;
fail[root] = root;
for(int i=0;i<26;i++){
if( next[root][i] == -1){
next[root][i] = root;
}else{
fail[next[root][i]] = root;
que.push(next[root][i]);
}
}
while(!que.empty()){
int now = que.front();
que.pop();
for(int i=0;i<26;i++){
int t = next[now][i];
if(t==-1){
next[now][i] = next[fail[now]][i];
}else{
fail[t] = next[fail[now]][i];
que.push(t);
}
}
}
}
void debug(){
for(int i=0;i
= 0; i--) { for(int j = i + 1; j < var; j++) x[i] -= x[j]*a[i][j]; x[i] /= a[i][i]; } return x[0]; } /// char buf[20]; int n; void getEquation(){ var = ac.L; equ = 0; memset(a,0,sizeof(a)); for(int i=0;i