Pagini recente » Cod sursa (job #228803) | Cod sursa (job #2947499) | Cod sursa (job #934190) | Cod sursa (job #2529827) | Cod sursa (job #1419492)
#include <cstdio>
#include <algorithm>
FILE* in=fopen("substr.in","r");
FILE* out=fopen("substr.out","w");
const int Q=17000;
char v[Q];
int n,k;
int d[15][Q];
struct tipe{
int a,b,wh;
} t[Q];
bool cmp(const tipe &x, const tipe &y)
{
if(x.a==y.a)
return x.b<y.b;
return x.a<y.a;
}
int l;
int lcp(int x, int y)
{
if(x==y)
return n-x+1;
int rez=0,go;
for(int k=l; k>=1; k--)
{
go=1<<k;
if(x+go<=n+1 && y+go<=n+1 && d[k][x]==d[k][y])
{
rez+=go;
x+=go;
y+=go;
}
}
if(v[x]==v[y])
rez++;
return rez;
}
void sortare()
{
for(int i=1; i<=n; i++)
d[0][i]=v[i];
t[0].a=-1;
for(int p=1; p<=n; p*=2)
{
l++;
for(int i=1; i<=n; i++)
{
t[i].a=d[l-1][i];
t[i].wh=i;
if(i+p<=n)
t[i].b=d[l-1][i+p];
else
t[i].b=-1;
}
std::sort(t+1,t+n+1,cmp);
int nxt=0;
for(int i=1; i<=n; i++)
{
if(t[i].a==t[i-1].a && t[i].b==t[i-1].b)
{
d[l][t[i].wh]=nxt;
}
else
{
d[l][t[i].wh]=++nxt;
}
}
}
}
int main()
{
fscanf(in,"%d%d\n",&n,&k);
fread(v+1,Q,1,in);
sortare();
int rez=0,act;
for(int i=k; i<=n; i++)
{
act=lcp(t[i-k+1].wh,t[i].wh);
if(act>rez)
rez=act;
}
fprintf(out,"%d\n",rez);
return 0;
}