SPOJ NSUBSTR 求每个对应的长度能产生的相同子串的最大个数

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

给定一个字符串,求每个对应的长度能产生的相同子串的最大个数

后缀自动机 首先要得出所有长度的子串在总串中的出现次数,其实只要得出最大长度就好,其他的长度可以由 dp[len]=max(dp[len],dp[len+1]) 得到。

后缀自动机上每个节点代表可以接收的最大长度后缀。那么可以 利用子节点往父节点更新就好。因为子节点出现的地方父节点也必然会出现,最后扫一遍len就行

#include <bits/stdc++.h>
using namespace std;

const int maxn = 500000+5;
int last,tail,Min[maxn];
int Max[maxn],cnt[maxn],vis[maxn];
int nxt[maxn][26],fail[maxn];

char sa[maxn],sb[maxn];
int Maxi[maxn];
int sz[maxn];
int b[maxn];
inline void build(char *s)
{
    while(*s)
    {
        int p=last,t=++tail,c=*s++-'a';
        sz[t]=1;
        Max[t]=Max[p]+1;
        while(p&&!nxt[p][c])
            nxt[p][c]=t,p=fail[p];
        if(p)
        {
            int q=nxt[p][c];
            if(Max[q]==Max[p]+1)
                fail[t]=q,Min[t]=Max[q]+1;
            else
            {
                int k=++tail;
                fail[k]=fail[q];
                fail[t]=fail[q]=k;
                Max[k]=Max[p]+1;
                memcpy(nxt[k],nxt[q],sizeof(nxt[q]));
                while(p&&nxt[p][c]==q)
                    nxt[p][c]=k,p=fail[p];
            }
        }
        else
            fail[t]=Min[t]=1;
        last=t;
    }
}

int q[maxn];


int f[maxn];
int main()
{
    scanf("%s",sa);
    last=1,tail=1;
    build(sa);
    int len=strlen(sa);
    for(int i=1;i<=tail;i++) cnt[Max[i]]++;
    for(int i=1;i<=len;i++) cnt[i]+=cnt[i-1];
    for(int i=1;i<=tail;i++) b[cnt[Max[i]]--]=i;
    for(int i=tail;i>=1;i--) sz[fail[b[i]]]+=sz[b[i]];
    for(int i=1;i<=tail;i++)
        f[Max[i]]=max(f[Max[i]],sz[i]);
    for(int i=len;i>=1;i--)
        f[i]=max(f[i],f[i+1]);
    for(int i=1;i<=len;i++)
        printf("%d\n",f[i] );
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值