SPOJ 8222 NSUBSTR 后缀自动机

求长度为i的出现次数最多的子串的次数。
即找出 min(s)imax(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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值