Pagini recente » Cod sursa (job #1972854) | Cod sursa (job #1662952) | Cod sursa (job #207091) | Cod sursa (job #2439084) | Cod sursa (job #1520674)
#include<cstdio>
#include<vector>
#define mod1 102931
#define mod2 104729
#define base 101
using namespace std;
char s[17000];
vector<pair<int,int> > table[103000];
int n;
int convert(char c){
if(c>='A'&&c<='Z')
return c-'A';
if(c>='a'&&c<='z')
return c-'a'+26;
return c-'0'+52;
}
int get_count(int hash1,int hash2){
int dim=table[hash1].size(),i;
for(i=0;i<dim;i++)
if(table[hash1][i].first==hash2){
table[hash1][i].second++;
return table[hash1][i].second;
}
table[hash1].push_back(make_pair(hash2,1));
return 1;
}
int get_max_count(int l){
int i,hash1=0,hash2=0,pow1=1,pow2=1,maximum,cnt;
maximum=-1;
for(i=0;i<mod1;i++)
table[i].clear();
for(i=0;i<l;i++){
hash1=(hash1*base+convert(s[i]))%mod1;
hash2=(hash2*base+convert(s[i]))%mod2;
if(i!=0){
pow1=(pow1*base)%mod1;
pow2=(pow2*base)%mod2;
}
}
cnt=get_count(hash1,hash2);
if(cnt>maximum)
maximum=cnt;
for(i=l;i<n;i++){
hash1=((hash1-pow1*convert(s[i-l]))%mod1+mod1)%mod1;
hash1=(hash1*base+convert(s[i]))%mod1;
hash2=((hash2-pow2*convert(s[i-l]))%mod2+mod2)%mod2;
hash2=(hash2*base+convert(s[i]))%mod2;
cnt=get_count(hash1,hash2);
if(cnt>maximum)
maximum=cnt;
}
return maximum;
}
int main(){
freopen("substr.in","r",stdin);
freopen("substr.out","w",stdout);
int k,l1,l2,m,answer=-1;
scanf("%d%d\n%s",&n,&k,&s);
l1=1;
l2=n;
while(l1<=l2){
m=(l1+l2)/2;
if(get_max_count(m)>=k){
l1=m+1;
if(m>answer)
answer=m;
}
else
l2=m-1;
}
printf("%d",answer);
return 0;
}