【模板】 ac自动机
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<queue> 5 #define maxn 5000000+10 6 using namespace std; 7 char str[maxn*2]; 8 struct node{ 9 int fail;//失配指针; 10 int cnt;//单词出现的次数; 11 int next[62];// 此节点的下一个(儿子)节点; 12 }trie[maxn];//节点结构体; 13 int k=0,ans=0; 14 queue<int> q;//队列:建失配指针使用; 15 void build_trie(int id,char *s)//id表示第几个结点,即所有字符串从第0个节点开始向下建;*s即表示这个字符串; 16 { 17 int len=strlen(s);//该字符串的长度;(相当于字符串最后一个字符的深度) 18 int j=0; 19 for(int i=0;i<len;i++){ 20 j=s[i]-'a'; 21 if(trie[id].next[j]==0)/*若此字母未出现在当前位置的下一深度*/ 22 { 23 trie[id].next[j]=++k;//当前节点对于j字母节点的位置;即j字母的节点序号;