la4513

//刘汝佳书上的用hash来写的,感觉有点拼人品呀,和X的取值的关系很大的
#include <iostream>
#include<string.h>
#include<stdio.h>
#include<algorithm>
using namespace std;
const int maxn=40005;
const int x=99;
int rank[maxn];
unsigned long long h[maxn],xp[maxn],hash[maxn];
int n,m,pos;
char str[maxn];
int cmp(const int& a,const int& b)
{
    return hash[a]<hash[b]||(hash[a]==hash[b]&&a<b);
}
int possible(int l)
{
    int i,c=0;
    pos=-1;
    for(i=0;i<=n-l;i++)
    {
        rank[i]=i;
        hash[i]=h[i]-h[i+l]*xp[l];
    }
    sort(rank,rank+n-l+1,cmp);
    for(i=0;i<=n-l;i++)
    {
        if(i==0||hash[rank[i]]!=hash[rank[i-1]])
            c=0;
        if(++c>=m)
            pos=max(pos,rank[i]);
    }
    return pos>=0;
}
int main()
{
    while(scanf("%d",&m)&&m)
    {
        int i;
        scanf("%s",str);
        n=strlen(str);
        h[n]=0;
        for(i=n-1;i>=0;i--)
        {
            h[i]=h[i+1]*x+(str[i]-'a');
        }
        xp[0]=1;
        for(i=1;i<=n;i++)
            xp[i]=xp[i-1]*x;
        if(!possible(1))
            printf("none\n");
        else
        {
            int L=1,R=n+1;
            while(R-L>1)
            {
                int mid=L+(R-L)/2;
                if(possible(mid))
                L=mid;
                else
                R=mid;
            }
            possible(L);
            printf("%d %d\n",L,pos);
        }


    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值