https://www.luogu.org/problemnew/show/P3966 因为文本串就是字典本身,所以这个和平时的AC自动机不太一样.平时的query要沿着fail树把子树的出现次数依次统计.但是这个的query就是对每个字典里的字符串搞一次.所以就直接按广搜的顺序反过来树形dp统计出子树中的出现次数,直接回答. #include<bits/stdc++.h> using namespace std; typedef long long ll; const int MAXN =…
题目链接 //删掉一个单词需要前移一段位置,用链表维护就好了 复杂度O(sum(len)) #include <cstdio> #include <cstring> #define S 26 const int N=1e5+7; int n; char s[N],p[N]; struct AC_Automaton { int cnt,son[N][S],fail[N],pos[N],q[N],dep[N]; bool val[N]; struct List { int l,r; }…
https://www.luogu.org/problemnew/show/P2444 有点恶心,不太明白fail的意义. #include<bits/stdc++.h> using namespace std; #define ll long long struct Trie{ ][],fail[],End[]; int root,L; int newnode(){ /*for(int i=0;i<26;i++) nex[L][i]=-1;*/ End[L++]=; ; } int c…
每次匹配都不停跳fail显然太慢了,于是在每个节点和fail指向的点连一条边,构成一棵树,在这棵树上差分一下就好了. AC自动机 就这个算法而言其实没用想象中那么难. #include <cstdio> #include <queue> #include <cstring> using namespace std; struct Node{ int fail, next[26], num; }AC[200010]; int n, u, cnt; queue <in…