//刘汝佳书上的用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;
}
la4513
最新推荐文章于 2017-10-30 14:46:13 发布