Pagini recente » Cod sursa (job #431536) | Cod sursa (job #2480790) | Cod sursa (job #2562996) | Cod sursa (job #87755) | Cod sursa (job #349136)
Cod sursa(job #349136)
#include <iostream>
#include <map>
using namespace std;
#define MOD1 31379
#define MOD2 3911
#define MAXN 16500
int n,len,lo,hi,ans;
char mat[MAXN];
bool can(int a){
map<int,int>mp;
int hash1,hash2,sub1,sub2,tmp;
hash1=hash2=0;
sub1=sub2=1;
for (int i=0;i<a;i++){
hash1=hash1*MOD1+mat[i];
hash2=hash2*MOD2+mat[i];
sub1*=MOD1;
sub2*=MOD2;
}
tmp=hash1*2+hash2;
if (mp[tmp]>=len-1) return 1;
mp[tmp]++;
for (int i=a;i<n;i++){
hash1=hash1*MOD1-sub1*mat[i-a]+mat[i];
hash2=hash2*MOD2-sub2*mat[i-a]+mat[i];
tmp=hash1*2+hash2;
if (mp[tmp]>=len-1) return 1;
mp[tmp]++;
}
return 0;
}
void open(){
freopen("substr.in","r",stdin);
freopen("substr.out","w",stdout);
}
int main(){
open();
scanf("%d%d",&n,&len);
scanf("%s",mat);
lo=1;hi=n-len+1;
while (1){
int mid=(lo+hi)/2;
if (can(mid)){
ans=mid;
lo=mid+1;
}
else {
hi=mid-1;
}
if (lo>hi) break;
}
printf("%d\n",ans);
//system("pause");
return 0;
}