Description
按顺序在一个序列的末尾插入数字,求每次插入后的本质不同子串个数
Solution
后缀自动机增量构造时,已经有的结点的 len 是不会再变化的,而 nq 结点不会产生贡献,于是每次加上 maxlen[newnode]-maxlen[link[newnode]] 即可
由于插入的是数字,所以需要用 map 存储
#include <bits/stdc++.h>
using namespace std;
const int Maxn = 200005;
struct Suffix_Automata {
int maxlen[Maxn], link[Maxn], Size, Last;
map<int,int> trans[Maxn];
int t[Maxn], a[Maxn], cnt[Maxn], f[Maxn];
long long ans;
Suffix_Automata() { Size = Last = 1; }
inline void Extend(int id) {
int cur = (++ Size), p;
maxlen[cur] = maxlen[Last] + 1;
cnt[cur] = 1;
for (p = Last; p && !trans[p][id]; p = link[p]) trans[p][id] = cur;
if (!p) link[cur] = 1;
else {
int q = trans[p][id];
if (maxlen[q] == maxlen[p] + 1) link[cur] = q;
else {
int clone = (++ Size);
maxlen[clone] = maxlen[p] + 1;
trans[clone] = trans[q];
link[clone] = link[q];
for (; p && trans[p][id] == q; p = link[p]) trans[p][id] = clone;
link[cur] = link[q] = clone;
}
}
Last = cur;
ans += maxlen[cur] - maxlen[link[cur]];
}
} sam;
int main() {
ios::sync_with_stdio(false);
int n;
cin>>n;
for(int i=0;i<n;i++) {
int x;
cin>>x;
sam.Extend(x), cout<<sam.ans<<endl;
}
}