Pagini recente » Cod sursa (job #1091014) | Cod sursa (job #1976549) | Cod sursa (job #442574) | Cod sursa (job #2242137) | Cod sursa (job #354579)
Cod sursa(job #354579)
#include <stdio.h>
#include <algorithm>
using namespace std;
#define maxn 17000
#define mlog 20
struct sufix
{
long d[2];
long poz;
};
long n, i, j, k, l, np, sol, aux, x, y;
char c[maxn], pas[mlog][maxn], ind[maxn];
sufix v[maxn];
inline int cmp(sufix a, sufix b)
{
if(a.d[0]!=b.d[0]) return a.d[0]<b.d[0];
return a.d[1]<b.d[1];
}
int main()
{
freopen("substr.in", "r", stdin);
freopen("substr.out", "w", stdout);
scanf("%d %d\n", &n, &k);
if(k==1)
{
printf("%d\n", n);
return 0;
}
for(i=1; i<=n; i++)
{
scanf("%c", &c[i]);
pas[0][i]=c[i];
}
for(i=1, l=1; l/2<=n; i++, l*=2)
{
for(j=1; j<=n; j++)
{
v[j].d[0]=pas[i-1][j];
if(j+l<=n)
v[j].d[1]=pas[i-1][j+l];
else
v[j].d[1]=-1;
v[j].poz=j;
}
sort(v+1, v+n+1, cmp);
for(j=1; j<=n; j++)
{
if(j>0 && v[j].d[0]==v[j-1].d[0] && v[j].d[1]==v[j-1].d[1])
{
pas[i][v[j].poz]=pas[i][v[j-1].poz];
}
else
{
pas[i][v[j].poz]=j;
}
}
}
np=i-1;
for(i=1; i<=n; i++)
{
ind[pas[np][i]]=i;
}
for(i=1; i<=n-k+1; i++)
{
aux=0;
x=ind[i];
y=ind[i+k-1];
for(j=np; j>=0; j--)
{
if(pas[j][x]==pas[j][y])
{
aux+=(1<<j);
x+=(1<<j);
y+=(1<<j);
}
}
if(aux>sol)
sol=aux;
}
printf("%d\n", sol);
return 0;
}