Dr. Ellie Arroway has established contact with an extraterrestrial civilization. However, all efforts to decode their messages have failed so far because, as luck would have it, they have stumbled upon a race of stuttering aliens! Her team has found out that, in every long enough message, the most important words appear repeated a certain number of times as a sequence of consecutive characters, even in the middle of other words. Furthermore, sometimes they use contractions in an obscure manner. For example, if they need to saybab twice, they might just send the message babab, which has been abbreviated because the second b of the first word can be reused as the first b of the second one.
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 of sthat appears at least m times. For example, in the message baaaababababbababbab, the length-5 word babab is contained 3 times, namely at positions 5, 7 and 12 (where indices start at zero). No substring appearing 3 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
The input contains several test cases. Each test case consists of a line with an integer m ( m![$ \ge$](https://icpcarchive.ecs.baylor.edu/external/45/4513img1.png)
Output
Print one line of output for each test case. If there is no solution, output none ; otherwise, print two integers in a line, separated by a space. The first integer denotes the maximum length of a substring appearing at least m times; the second integer gives the rightmost possible starting position of such a substring.Sample Input
3 baaaababababbababbab 11 baaaababababbababbab 3 cccccc 0
Sample Output
5 12 none 4 2
这道题目可以用后缀数组+LCP来做,具体做法考:poj 3261
不过这题LCP也可用hash来做,参考大白书代码。
代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#define ull unsigned long long
#define Maxn 40010
using namespace std;
const int x=131;
ull h[Maxn],xp[Maxn],HASH[Maxn];
int rk[Maxn];
char s[Maxn];
bool cmp(const int &a,const int &b){
if(HASH[a]==HASH[b]) return a<b;
return HASH[a]<HASH[b];
}
int check(int l,int m,int n){
for(int i=0;i<=n-l;i++){
rk[i]=i;
HASH[i]=h[i]-h[i+l]*xp[l];
}
sort(rk,rk+n-l+1,cmp);
int cnt=0,pos=-1;
for(int i=0;i<=n-l;i++){
if(i==0||HASH[rk[i]]!=HASH[rk[i-1]]) cnt=0;
if(++cnt>=m) pos=max(pos,rk[i]);
}
return pos;
}
int main()
{
int m;
while(scanf("%d",&m),m){
scanf("%s",s);
int n=strlen(s);
h[n]=0;
for(int i=n-1;i>=0;i--) h[i]=h[i+1]*x+(s[i]-'a');
xp[0]=1;
for(int i=1;i<=n;i++) xp[i]=xp[i-1]*x;
if(check(1,m,n)==-1) {puts("none");continue;}
int l=1,r=n;
while(l<r){
int mid=l+r+1>>1;
if(check(mid,m,n)!=-1) l=mid;
else r=mid-1;
}
printf("%d %d\n",l,check(l,m,n));
}
return 0;
}