【AC自动机】单词
【题目链接】 https://loj.ac/problem/10060 【题意】 某人读论文,一篇论文是由许多单词组成。但他发现一个单词会在论文中出现很多次,现在想知道每个单词分别在论文中出现多少次。 【题解】 与AC自动机模板题3类似,但是这个题目记录的子串是不能重叠的。 利用下标从后往前历遍,然后把对应的位置的地方叠加到公共后缀。原因是:如果该单词出现过,那么对应的公共后缀肯定也出现过。 1 #include<cstdio> 2 #include<algorithm> 3 using namespace std; 4 const int N = 1005; 5 const int M = 1e6+10; 6 char S[N][205]; 7 int Trie[M][26],fail[M],End[M],Sz[M]; 8 int Q[M],Head,Tail; 9 int head[M],nxt[M],to[M],cnt; 10 int n,idx=1; 11 12 void Add_edge(int u,int v); 13 void dfs(int u); 14 void Insert( char s[] , int Id){ 15 int p = 1 ; 16 for(int i=0;s[i];i++){ 17 int t = s[i] - 'a' ; 18 if(