标签:ini max 后缀自动机 else 答案 += init auto print
K = 1..length(S)求出所有长度为K的子串中出现次数最多的子串的出现次数
| endpos(st) |就是st这个状态包含的子串在S中出现的次数
在parent tree上自底向上累加| endpos |,考虑特殊情况:当前状态包含S的前缀,则额外+1,最后统计答案即可
#include <bits/stdc++.h>
#define Cpy(a, b) memcpy(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int Maxn = 2000010;
const int maxn = 1000010;
bool vis[Maxn];
int cnt[Maxn], in[Maxn];
struct Suffix_Automata
{
int maxlen[Maxn], trans[Maxn][26], link[Maxn], Size, Last;
void init(){Size = Last = 1;}
inline void Extend(int id)
{
int cur = (++Size), p;
vis[cur] = true;
maxlen[cur] = maxlen[Last] + 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;
Cpy(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;
}
} T;
char s[maxn];
void tp()
{
queue<int> q;
for (int i = 1; i <= T.Size; i++)
if (in[i] == 0)
{
q.push(i);
cnt[i] = 1;
}
while (!q.empty())
{
int p = q.front();
q.pop();
int y = T.link[p];
if (y == 0)
continue;
cnt[y] += cnt[p];
in[y]--;
if (in[y] == 0)
q.push(y);
}
}
int ans[maxn];
int main()
{
scanf("%s", s);
T.init();
int len = strlen(s);
for (int i = 0; i < len; i++)
T.Extend(s[i] - 'a');
for (int i = 1; i <= T.Size; i++)
in[T.link[i]]++;
for (int i = 1; i < T.Size; i++)
if (in[i] != 0 && vis[i] == true)
cnt[i] = 1;
tp();
for (int i = 1; i <= T.Size; i++)
ans[T.maxlen[i]] = max(ans[T.maxlen[i]], cnt[i]);
for (int i = len - 1; i >= 1; i--)
ans[i] = max(ans[i], ans[i + 1]);
for (int i = 1; i <= len; i++)
printf("%d\n", ans[i]);
return 0;
}
标签:ini max 后缀自动机 else 答案 += init auto print
原文地址:https://www.cnblogs.com/Zeronera/p/11557819.html