求长度为i的出现次数最多的子串的次数。
即找出
min(s)≤i≤max(s)
的状态
i
<script type="math/tex" id="MathJax-Element-20">i</script>,使其Right集合元素个数最多。。
首先可以计算出每个状态的Right集合大小,即Parent树的子树的叶子节点数。
一直internal error也是坑。。
手动内联后就神奇地AC了?!
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
#define FOR(i,j,k) for(i=j;i<=k;++i)
const int rt = 1, N = 500005;
int last = 1, cnt = 1, len = 0;
int ch[N][26], fa[N], ma[N], v[N], b[N], dp[N], bucket[N];
char str[N];
void add(char c) {
int np = ++cnt, p = last, q, nq; last = np; ma[np] = ++len; v[np] = 1;
while (p && !ch[p][c]) ch[p][c] = np, p = fa[p];
if (!p) fa[np] = rt;
else {
q = ch[p][c];
if (ma[q] == ma[p] + 1) fa[np] = q;
else {
nq = ++cnt; memcpy(ch[nq], ch[q], sizeof ch[q]);
ma[nq] = ma[p] + 1; fa[nq] = fa[q]; fa[q] = fa[np] = nq; v[nq] = 0;
while (p && ch[p][c] == q) ch[p][c] = nq, p = fa[p];
}
}
}
int main() {
int i, n;
scanf("%s", str + 1);
n = strlen(str + 1);
FOR(i,1,n) add(str[i] - 'a');
FOR(i,1,cnt) ++bucket[ma[i]];
FOR(i,1,n) bucket[i] += bucket[i - 1];
for(i=cnt;i;--i) b[bucket[ma[i]]--] = i;
for(i=cnt;i;--i) v[fa[b[i]]] += v[b[i]];
FOR(i,1,cnt) dp[ma[i]] = max(dp[ma[i]], v[i]);
for(i=n;i;--i) dp[i] = max(dp[i], dp[i + 1]);
FOR(i,1,n) printf("%d\n", dp[i]);
return 0;
}
NSUBSTR - Substrings
You are given a string S which consists of 250000 lowercase latin letters at most. We define F(x) as the maximal number of times that some string with length x appears in S. For example for string ‘ababa’ F(3) will be 2 because there is a string ‘aba’ that occurs twice. Your task is to output F(i) for every i so that 1<=i<=|S|.
Input
String S consists of at most 250000 lowercase latin letters.
Output
Output |S| lines. On the i-th line output F(i).
Example
Input:
ababa
Output:
3
2
2
1
1