Substrings(SPOJ8222) (sam(后缀自动机))

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

题意:

给定一个字符串\(S\),令\(F(x)\)表示\(S\)中所有长度为\(x\)的子串出现最多的次数,

题解:

把串塞进一个后缀自动机,在\(parent\)树上反向拓扑,求出每个长度时的最大值。

#include<bits/stdc++.h>
using namespace std;
const int N=2000010;
char s[N];
int a[N],c[N],ans[N];
struct SAM{
    int last,cnt;
    int size[N],ch[N][26],fa[N<<1],l[N<<1];
    void ins(int c){
        int p=last,np=++cnt;last=np;l[np]=l[p]+1;
        for(;p&&!ch[p][c];p=fa[p])ch[p][c]=np;
        if(!p)fa[np]=1;
        else{
            int q=ch[p][c];
            if(l[p]+1==l[q])fa[np]=q;
            else{
                int nq=++cnt;l[nq]=l[p]+1;
                memcpy(ch[nq],ch[q],sizeof ch[q]);
                fa[nq]=fa[q];fa[q]=fa[np]=nq;
                for(;ch[p][c]==q;p=fa[p])ch[p][c]=nq;
            }
        }
        size[np]=1;
    }
    void build(char s[]){
        int len=strlen(s+1);
        last=cnt=1;
        for(int i=1;i<=len;++i)ins(s[i]-'a');
    }
    void calc(int len){
        for(int i=1;i<=cnt;++i)c[l[i]]++;
        for(int i=1;i<=cnt;++i)c[i]+=c[i-1];
        for(int i=1;i<=cnt;++i)a[c[l[i]]--]=i;
        for(int i=cnt;i;--i){
            int p=a[i];
            size[fa[p]]+=size[p];
            ans[l[p]]=max(ans[l[p]],size[p]);
        }
        for(int i=1;i<=len;++i){
            printf("%d\n",ans[i]);
        }
    }
}sam;
int main(){
    cin>>s+1;
    sam.build(s);
    sam.calc(strlen(s+1));
}

转载于:https://www.cnblogs.com/zhenglier/p/10097776.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值