Cod sursa(job #2343238)

Utilizator AlexPascu007Pascu Ionut Alexandru AlexPascu007 Data 13 februarie 2019 20:21:31
Problema Substr Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.09 kb
#include <fstream>
#include <algorithm>
#define DIM (1<<15)+5
#define log 16
#define v1 first.first
#define v2 first.second
#define poz second
using namespace std;
ifstream fin("substr.in");
ofstream fout("substr.out");
int n,k,i,j,v[DIM],p[log][DIM],e,lung,sol;
char s[DIM];
pair< pair<int,int>, int> L[DIM];
int lcp(int i,int j) {
	if (i==j)
		return n-i;
	int f=e, putere=(1<<e), sol=0;
	while (putere) {
		if (p[f][i]==p[f][j]) {
			sol+=putere;
			i+=putere, j+=putere;
		}
		f--, putere/=2;
	}
	return sol;
}
int main() {
	fin>>n>>k;
	fin>>s;
	for (i=0;i<n;i++)
		p[0][i]=s[i]-'0';
	for (e=0,lung=1;lung<=n;e++,lung*=2) {
		for (i=0;i<n;i++) {
			L[i].v1=p[e][i];
			if (i+lung<n)
				L[i].v2=p[e][i+lung];
			else
				L[i].v2=-1;
			L[i].poz=i;
		}
		sort(L,L+n);
		p[e+1][L[0].poz]=0;
		for (i=1;i<n;i++)
			if (L[i].first==L[i-1].first)
				p[e+1][L[i].poz]=p[e+1][L[i-1].poz];
			else
				p[e+1][L[i].poz]=p[e+1][L[i-1].poz]+1;
	}
	for (i=0;i<n;i++)
		v[p[e][i]]=i;
	for (i=0;i+k-1<n;i++)
		sol=max(sol,lcp(v[i],v[i+k-1]));
	fout<<sol;
	return 0;
}