Pagini recente » Cod sursa (job #1621934) | Cod sursa (job #1897613) | Cod sursa (job #1274545) | Cod sursa (job #2290255) | Cod sursa (job #176126)
Cod sursa(job #176126)
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
#define DxBG
#define FL
#define fin "substr.in"
#define fout "substr.out"
#define mp make_pair
#define pb push_back
#define f first
#define s second
const int Nmax = 17000;
const int LgMax = 20;
int N,K,best,lim,P[LgMax][Nmax];
char buff[Nmax];
vector < pair < pair <int,int> ,int> > v;
int lcp(int x,int y)
{
int step,cnt = 0;
if ( x == y ) return N - x;
for ( step = lim; step >= 0 && x < N && y < N; --step )
{
if ( P[step][x] == P[step][y] )
cnt += 1 << step, x += 1 << step, y += 1 << step;
#ifdef DBG
printf("%d %d %d\n",cnt,x,y);
#endif
}
#ifdef DBG
printf("--\n");
#endif
return cnt;
}
void readdata_solve()
{
int i,step,cnt;
#ifdef FL
freopen(fin,"r",stdin);
freopen(fout,"w",stdout);
#endif
scanf("%d%d\n",&N,&K);
fgets(buff,Nmax,stdin);
for ( i = 0; i < N; ++i )
P[0][i]=buff[i]-'a';
//build suffarray
for ( step = 1, cnt = 1; cnt <= N; cnt <<= 1, ++step )
{
v.clear();
for ( i = 0; i < N; ++i )
v.pb(mp(mp(P[step-1][i],(i+cnt<N)?(P[step-1][i+cnt]):(-1)),i));
sort(v.begin(),v.end());
for ( i = 0; i < N; ++i )
P[step][v[i].s] = ( i > 0 && v[i].f.f == v[i-1].f.f && v[i].f.s == v[i-1].f.s ) ? ( P[step][v[i-1].s] ) : ( i );
}
lim = step - 1;
#ifdef DBG
for ( step = 0; step <= lim; ++step,printf("\n") )
for ( i = 0; i < N; ++i )
printf("%d ",P[step][i]);
#endif
//query lcp
for ( i = 0; i + K - 1 < N; ++i )
best = max(best,lcp(v[i].s,v[i+K-1].s));
printf("%d\n",best);
}
int main()
{
readdata_solve();
return 0;
}