Cod sursa(job #1131587)

Utilizator otto1Palaga Vicentiu-Octavian otto1 Data 28 februarie 2014 21:51:51
Problema Substr Scor 100
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2013 Marime 1.04 kb
#include<fstream>
#define N 100100
#include<algorithm>
#include<cstring>
#define lm 20
using namespace std;
ifstream f("substr.in");
ofstream g("substr.out");
struct el{ int n1,n0,pz; }l[N];
int i,p[lm][N],n,k,po,t,sol;
char s[N];
bool cmp(const el &A,const el &B){ if(A.n0==B.n0) return A.n1<B.n1; return A.n0<B.n0; }
int cb(int x,int y)
{
	int j,rez=0,po;
	for(j=t,po=(1<<t);j>=0&&x<=n&&y<=n;--j,po>>=1)
		if(p[j][x]==p[j][y]&&x+po<=n+1&&y+po<=n+1)
		{
			x+=po;
			y+=po;
			rez+=po;
		}
	return rez;
}
int main ()
{
	f>>n>>k;
	f>>(s+1);
	for(i=1;i<=n;++i)
		p[0][i]=s[i]-'a';
	for(po=1,t=1;(po>>1)<n;po<<=1,t++)
	{
		for(i=1;i<=n;++i)
		{
			l[i].n0=p[t-1][i];
			if(i+po<=n)
				l[i].n1=p[t-1][i+po];
			else
				l[i].n1=0;
			l[i].pz=i;
		}
		sort(l+1,l+n+1,cmp);
		for(i=1;i<=n;++i)
		if(i!=1&&l[i].n1==l[i-1].n1&&l[i].n0==l[i-1].n0)
			p[t][l[i].pz]=p[t][l[i-1].pz];
		else
			p[t][l[i].pz]=i;
	}
	t--;
	sol=0;
	for(i=1;i<=n-k+1;++i)
	{
		sol=max(sol,cb(l[i].pz,l[i+k-1].pz));
	}
	g<<sol;
	return 0;
}