ac自动机
https://www.cnblogs.com/sclbgw7/p/9875671.html 讲的好的博客。 只有fail指针的模板,好像用不到,一般都是用last指针的模板。 void build() { queue<int>q; q.push(1); while(!q.empty()) { int x=q.front();q.pop(); for(int i=0;i<26;++i) { int c=ch[x][i]; if(!c){ch[x][i]=ch[fail[x]][i];continue;}//关键,把子节点改成fail节点的子节点 q.push(c); int fa=fail[x]; while(fa&&!ch[fa][i])fa=fail[fa]; fail[c]=ch[fa][i]; } } } https://cn.vjudge.net/contest/301351#problem/A 习题 最基础的模板,即求一个文本串有多少个匹配的模式串。 #include<queue> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn = 5e5 + 5; const int N = 1e6 + 5; int tree[maxn][27];