后缀数据结构模板2
普通后缀自动机模板(新)
int len[2000010], link[2000010], tot, last;
map<char, int> nex[2000010];
void init() { tot = 1, last = 1; len[1] = 0, link[1] = -1; nex[1].clear(); }
void insert(char ch)
{
int x = ++tot, p = last; len[x] = len[last] + 1, nex[x].clear();
while (p != -1 && nex[p].count(ch) == false) nex[p][ch] = x, p = link[p];
if (p == -1) link[x] = 1;
else
{
int q = nex[p][ch];
if (len[p] + 1 == len[q]) link[x] = q;
else
{
int t = ++tot; len[t] = len[p] + 1, link[t] = link[q], nex[t] = nex[q];
while (p != -1 && nex[p][ch] == q) nex[p][ch] = t, p = link[p];
link[q] = link[x] = t;
}
}
last = x;
}