spoj 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).

建出来后缀自动机,按照parent树上的拓扑序统计每个点right集合的大小,去更新这个点max的答案。
注意parent树的拓扑序不是自动机的拓扑序,需要按照max的大小顺序更新。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char s[250010];
int fa[500010],son[500010][30],val[500010],num[500010],a[500010],cnt[500010],ans[250010],n,tot,r;
int main()
{
    int p,np,q,nq,c,last;
    scanf("%s",s+1);
    n=strlen(s+1);
    last=r=tot=1;
    for (int i=1;i<=n;i++)
    {
        c=s[i]-'a'+1;
        p=last;
        val[np=++tot]=val[p]+1;
        while (p&&!son[p][c])
        {
            son[p][c]=np;
            p=fa[p];
        }
        if (!p) fa[np]=r;
        else
        {
            q=son[p][c];
            if (val[p]+1==val[q]) fa[np]=q;
            else
            {
                val[nq=++tot]=val[p]+1;
                for (int j=1;j<=26;j++) son[nq][j]=son[q][j];
                fa[nq]=fa[q];
                fa[q]=nq;
                fa[np]=nq;
                while (p&&son[p][c]==q)
                {
                    son[p][c]=nq;
                    p=fa[p];
                }
            }
        }
        last=np;
    }
    p=r;
    for (int i=1;i<=n;i++)
    {
        p=son[p][s[i]-'a'+1];
        cnt[p]++;
    }
    for (int i=1;i<=tot;i++) num[val[i]]++;
    for (int i=1;i<=n;i++) num[i]+=num[i-1];
    for (int i=1;i<=tot;i++) a[num[val[i]]--]=i;
    for (int i=tot;i;i--) cnt[fa[a[i]]]+=cnt[a[i]];
    for (int i=1;i<=tot;i++) ans[val[i]]=max(ans[val[i]],cnt[i]);
    for (int i=n;i;i--) ans[i]=max(ans[i],ans[i+1]);
    for (int i=1;i<=n;i++) printf("%d\n",ans[i]);
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值