Pagini recente » Cod sursa (job #88358) | Cod sursa (job #245690) | Cod sursa (job #2658066) | Cod sursa (job #2662254) | Cod sursa (job #349134)
Cod sursa(job #349134)
#include <iostream>
#include <map>
using namespace std;
#define MOD1 31379
#define MOD2 3911
#define MAXN 16500
int n,len,lo,hi,ans,arr[MAXN],cnt;
char mat[MAXN];
bool can(int a){
map<int,int>mp;
int hash1,hash2,sub1,sub2,tmp,k;
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+hash2;
if (!mp[tmp]) mp[tmp]=cnt++;
k=mp[tmp];
if (arr[k]>=len-1) return 1;
arr[k]++;
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+hash2;
if (!mp[tmp]) mp[tmp]=cnt++;
k=mp[tmp];
if (arr[k]>=len-1) return 1;
arr[k]++;
}
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;
while (1){
memset(arr,0,sizeof(arr));
cnt=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;
}