Pagini recente » Cod sursa (job #1993809) | Cod sursa (job #1932528) | Cod sursa (job #1697149) | Cod sursa (job #697438) | Cod sursa (job #1083539)
#include<fstream>
#include <algorithm>
#define n_maxx 17000
#define mlog 20
using namespace std;
ifstream f("substr.in");
ofstream g("substr.out");
struct var1
{
long d[2];
long poz;
};
long n, i, j, k, l, np, sol, aux, x, y;
char c[n_maxx];
short int pas[mlog][n_maxx], ind[n_maxx];
var1 v[n_maxx];
int funn(var1 a, var1 b)
{
if(a.d[0]!=b.d[0])
return a.d[0]<b.d[0];
return a.d[1]<b.d[1];
}
int comare(long a, long b)
{
return pas[np][a]<pas[np][b];
}
int main()
{
f>>n>>k;
if(k==1)
{
g<<n;
return 0;
}
for(i=1; i<=n; i++)
{
f>>c[i];
pas[0][i]=c[i]-'a';
}
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, funn);
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[i]=i;
}
sort(ind+1, ind+n+1, comare);
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;
}
g<<sol;
return 0;
}