Pagini recente » Cod sursa (job #3172209) | Cod sursa (job #2204805) | Cod sursa (job #1300846) | Cod sursa (job #1990361) | Cod sursa (job #1817192)
#include <cstdio>
#include <algorithm>
#define NMax 16390
using namespace std;
struct abc{ int x,y,z; };
abc v[NMax+1];
const int P = 1399009;
const int Q = 147181;
const int K = 4733;
char s[NMax+1];
int pwrP[NMax+1];
int pwrQ[NMax+1];
int pwrK[NMax+1];
int ch[256];
int N,T;
bool cmp(const abc A, const abc B)
{
if( A.x==B.x && A.y==B.y ) return A.z<B.z;
if( A.x==B.x ) return A.y<B.y;
return A.x<B.x;
}
bool is_good(int f)
{
int i,j,M,a,b,c;
abc temp;
a = b = c = 0;
for(M = i = 0; i < f; ++i)
{
a = ( a * 67 + ch[ s[i] ] ) % P;
b = ( b * 67 + ch[ s[i] ] ) % Q;
c = ( c * 67 + ch[ s[i] ] ) % K;
}
temp.x = a; temp.y = b; temp.z = c;
v[++M] = temp;
for(i = f; i < N; ++i)
{
a = ( 1LL * ( a - ch[ s[i-f] ] * pwrP[f-1] + 70*P ) * 67 + ch[ s[i] ] ) % P;
b = ( 1LL * ( b - ch[ s[i-f] ] * pwrQ[f-1] + 70*Q ) * 67 + ch[ s[i] ] ) % Q;
c = ( 1LL * ( c - ch[ s[i-f] ] * pwrK[f-1] + 70*K ) * 67 + ch[ s[i] ] ) % K;
temp.x = a; temp.y = b; temp.z = c;
v[++M] = temp;
}
sort(v+1,v+M+1,cmp);
for(i = 1; i <= M; )
{
for(j = i; j <= M && v[i].x==v[j].x && v[i].y==v[j].y && v[i].z==v[j].z; ++j);
if( j - i >= T ) return 1;
i = j;
}
return 0;
}
int main(){
freopen("substr.in","r",stdin);
freopen("substr.out","w",stdout);
int f,st,dr,mid,i,t=-1;
scanf("%d %d\n",&N,&T);
fgets(s,NMax,stdin);
for(i = 'a'; i <= 'z'; ++i) ch[i] = ++t;
for(i = 'A'; i <= 'Z'; ++i) ch[i] = ++t;
for(i = '0'; i <= '9'; ++i) ch[i] = ++t;
pwrP[0] = pwrQ[0] = pwrK[0] = 1;
for(i = 1; i < NMax; ++i)
{
pwrP[i] = ( pwrP[i-1] * 67 ) % P;
pwrQ[i] = ( pwrQ[i-1] * 67 ) % Q;
pwrK[i] = ( pwrK[i-1] * 67 ) % K;
}
for(f = 0, st = 1, dr = N; st <= dr; )
{
mid = (st+dr)/2;
if( !is_good(mid) ) dr = mid-1;
else { f = mid; st = mid+1; }
}
printf("%d\n",f);
return 0;
}