小小总结
- 千万记住insert完以后要build!!!老是忘掉QAQ
- insert函数就只是trie数的插入函数,可在结尾根据条件打一些标记
- build函数(构建fail树),主要利用BFS
- match函数中跑fail树其实就是反复跑后缀
- fail树上倒过来建树以后可以进行很多高端操作,比如阿狸的打字机(也许还能树剖一下QAQ)
- match函数中跑fail树过程可以转化为打标记,而避免重复跑一个相同的后缀,常见优化(见下面的二次加强版板子题)
- AC自动机常与数位DP结合,毕竟数字和字符串长得差不多
洛谷-P3808 简单板子题
//#pragma comment(linker, "/STACK:102400000,102400000")
#include "bits/stdc++.h"
#define pb push_back
#define ls l,m,now<<1
#define rs m+1,r,now<<1|1
#define hhh printf("hhh\n")
#define see(x) (cerr<<(#x)<<'='<<(x)<<endl)
using namespace std;
typedef long long ll;
typedef pair<int,int> pr;
inline int read() {int x=0;char c=getchar();while(c<'0'||c>'9')c=getchar();while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();return x;}
const int maxn = 1e5+10;
const int mod = 1e9+7;
const double eps = 1e-9;
int trie[maxn][26], cnt;
int e[maxn];
int fail[maxn];
void insert(char *s) {
int p=0;
for(int i=0; s[i]; ++i) {
int k=s[i]-'a';
if(!trie[p][k]) trie[p][k]=++cnt;
p=trie[p][k];
}
e[p]++;
}
void