您的位置:首页 > 其它

SPOJ 8222 NSUBSTR 后缀自动机

2016-04-15 20:48 399 查看
求长度为i的出现次数最多的子串的次数。

即找出min(s)≤i≤max(s)的状态i,使其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
[26], fa
, ma
, v
, b
, dp
, bucket
;
char str
;
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: