Pagini recente » Cod sursa (job #3168266) | Cod sursa (job #2981139) | Cod sursa (job #1863473) | Cod sursa (job #1612578) | Cod sursa (job #482165)
Cod sursa(job #482165)
#include<stdio.h>
#include<algorithm>
using namespace std;
const char IN[] = "substr.in";
const char OUT[] = "substr.out";
const int NMAX = 16985;
const int LGMAX = 20;
struct S{
int c1 , c2 , act;
} X[NMAX];
char A[NMAX];
int SA[LGMAX][NMAX] , N , K , pas , REZ , cnt ;
int sort_comp(S a , S b)
{
if(a.c1 == b.c1) return a.c2 < b.c2;
else return a.c1 < b.c1;
}
int LCP(int x ,int y)
{
int k , l = 0;
if(x == y) return N - x + 1;
for( k = pas - 1 ; k >= 0 && x <= N && y <= N ; --k)
if(SA[k][x] == SA[k][y])
x += (1<<k) , y += (1<<k) , l += (1<<k) ;
return l;
}
int main()
{
freopen(IN , "r" , stdin);
freopen(OUT , "w" , stdout);
scanf("%d %d\n" , &N , &K);
gets(A);
for(int i = 0 ; i < N ; ++i)
SA[0][i] = A[i] - '0';
for( pas = 1 , cnt = 1 ; ( cnt >> 1 ) < N ; cnt <<= 1 , ++pas)
{
for(int i = 0 ; i < N ; ++i)
{
X[i].c1 = SA[pas - 1][i];
X[i].c2 = (i + cnt < N ) ? SA[pas - 1][i + cnt] : -1;
X[i].act = i;
}
sort(X , X + N , sort_comp);
for(int i = 0 ; i < N ; ++i)
if(i > 0 && X[i].c1 == X[i - 1].c1 && X[i].c2 == X[i - 1].c2) SA[pas][X[i].act] = SA[pas ][X[i - 1].act];
else SA[pas][X[i].act] = i;
}
for(int i = 0 ; i + K < N ; ++i)
{
int temp = LCP (X[i].act , X[i + K - 1].act);
if(temp > REZ) REZ = temp;
}
printf("%d\n",REZ);
return 0;
}