後綴自動機板子
阿新 • • 發佈:2018-04-17
coder ans problem ems reg ble can spa problems
後綴自動機
是什麽?
博主不想寫反正沒人看
直接去\(hihocoder\)
1
2
模板
# include <bits/stdc++.h>
# define IL inline
# define RG register
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
template <class Int>
IL void Input(RG Int &x){
RG int z = 1; RG char c = getchar(); x = 0 ;
for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
x *= z;
}
const int maxn(2e6 + 5);
int n, trans[26][maxn], fa[maxn], len[maxn], tot = 1 , last = 1;
char s[maxn];
IL void Extend(RG int c){
RG int np = ++tot, p = last; last = np;
len[np] = len[p] + 1;
while(p && !trans[c][p]) trans[c][p] = np, p = fa[p];
if(!p) fa[np] = 1;
else{
RG int q = trans[c][p];
if(len[p] + 1 == len[q]) fa[np] = q;
else {
RG int nq = ++tot;
len[nq] = len[p] + 1, fa[nq] = fa[q];
for(RG int i = 0; i < 26; ++i) trans[i][nq] = trans[i][q];
fa[q] = fa[np] = nq;
while(p && trans[c][p] == q) trans[c][p] = nq, p = fa[p];
}
}
}
int main(RG int argc, RG char* argv[]){
scanf(" %s", s), n = strlen(s);
for(RG int i = 0; i < n; ++i) Extend(s[i] - 'a');
return 0;
}
後綴自動機板子