Description

Thus, the message contains possibly overlapping repetitions of the same words over and over again. As a result, Ellie turns to you, S.R. Hadden, for help in identifying the gist of the message.
Given an integer m, and a string s, representing the message, your task is to find the longest substring ofs that appears at least m times. For example, in the messagebaaaababababbababbab, the length-5 wordbabab is contained 3 times, namely at positions5, 7 and 12 (where indices start at zero). No substring appearing3 or more times is longer (see the first example from the sample input). On the other hand, no substring appears11 times or more (see example 2).
In case there are several solutions, the substring with the rightmost occurrence is preferred (see example3).
Input

Output
Sample Input
3 baaaababababbababbab 11 baaaababababbababbab 3 cccccc 0
Sample Output
5 12 none 4 2
Source
解析
为每个后缀弄个H值(H[i]=H[i+1]*x+s[i]),对于串s[4]=abcd
H[4]=0
H[3]=s[3] d
H[2]=s[3]*x +s[2] cd
H[1]=s[3]*x^2+s[2]*x +s[1] bcd
H[0]=s[3]*x^3+s[2]*x^2+s[1]*x+s[0] abcd
对于一段长L的子串s[k,k+L)的hash值是H[k]-H[k+L]*x^L
展开:
s[k+L-1]*x^(L-1)+s[k+L-2]*x^(L-2)+...+s[k+1]*x+s[k]
用hash实现最长公共前缀……注意hash的取值。开始用random生成hash,wa了好多发……
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<ctime>
#include<cstdlib>
using namespace std;
char s[40100];
unsigned long long H[40100],hash[40100],xp[40100],x;
int N,rank[40100],pos,M;
void prework()
{
scanf("%s",s); N=strlen(s);
//find H
H[N]=0;
for(int i=N-1;i>=0;i--) H[i]=H[i+1]*x+(s[i]-97);
}
int cmp(int a,int b)
{return hash[a]<hash[b] || (hash[a]==hash[b] && a<b);}
bool Check(int x)
{
for(int i=0;i<=N-x;i++) rank[i]=i,hash[i]=H[i]-H[i+x]*xp[x];
sort(rank,rank+N-x+1,cmp);
int sum=0; pos=-1;
for(int i=0;i<=N-x;i++)
{
if(i>0&&hash[rank[i]]!=hash[rank[i-1]]) sum=0;
if(++sum>=M) pos=max(pos,rank[i]);
}
return pos>=0;
}
void Binary_Search()
{
int L=1,R=N+1,mid;//[l,r)
while(L<R)
{
mid=(L+R)>>1;
if(Check(mid)) L=mid+1;
else R=mid;
}
if(--L) {Check(L);printf("%d %d\n",L,pos);}
else puts("none");
}
int main()
{
srand(time(NULL));
x=(int)((double)rand()/(RAND_MAX+1)*100)+100;
x=31;
//find x^L
xp[0]=1;
for(int i=1;i<=40005;i++) xp[i]=xp[i-1]*x;
while(scanf("%d",&M)==1 && M)
{
prework();
Binary_Search();
}
return 0;
}