Pagini recente » Cod sursa (job #2172011) | Cod sursa (job #1945591) | Cod sursa (job #1074494) | Cod sursa (job #715140) | Cod sursa (job #474626)
Cod sursa(job #474626)
#include <cstdio>
#include <cstring>
#define file_in "substr.in"
#define file_out "substr.out"
#define mod 666013
#define mod1 33
#define nmax 20100
int n,k;
char s[nmax];
int hash[mod+20];
void citire()
{
freopen(file_in,"r",stdin);
freopen(file_out,"w",stdout);
scanf("%d %d\n", &n, &k);
gets(s);
}
int verif(int l)
{
memset(hash,0,sizeof(hash));
int x=0,put=1,i;
for (i=0;i<l;++i)
{
if (i>0)
put*=mod1;
if (put>=mod)
put%=mod;
x*=mod1;
x+=s[i]-'a'+1;
if (x>=mod)
x%=mod;
}
hash[x]++;
for (i=l;i<=n;++i)
{
x-=(put*(s[i-l]-'a'+1)%mod);
if (x<0)
x+=mod;
if (x>=mod)
x%=mod;
x*=mod1;
x+=s[i]-'a'+1;
if (x>=mod)
x%=mod;
hash[x]++;
if (hash[x]>=k)
return 1;
}
return 0;
}
void solve()
{
int i,step;
for (step=1;step<=n;step<<=1);
for (i=0;step;step>>=1)
if (verif(i+step))
i+=step;
printf("%d\n", i);
}
int main()
{
citire();
solve();
fclose(stdin);
fclose(stdout);
return 0;
}