Cod sursa(job #1467415)

Utilizator theprdvtheprdv theprdv Data 3 august 2015 13:08:07
Problema Substr Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.16 kb
#define _CRT_SECURE_NO_DEPRECATE
#include <cstdio>
#include <algorithm>
#define MAXN 1 << 14
#define LG 15

using namespace std;

int K, N, maxx;
char A[MAXN];
int P[LG][MAXN];

struct entry{
	int nr[2], p;
} L[MAXN];

inline bool cmp(const entry a, const entry b){
	return a.nr[0] != b.nr[0] ? a.nr[0] < b.nr[0] : a.nr[1] < b.nr[1];
}

inline bool eq(int j){
	if (!j) return 0;
	return L[j].nr[0] == L[j - 1].nr[0] && L[j].nr[1] == L[j - 1].nr[1] ? 1 : 0;
}

int main() {
	freopen("substr.in", "r", stdin);
	freopen("substr.out", "w", stdout);

	scanf("%d %d\n", &N, &K);
	gets(A);

	for (int i = 0; i < N; ++i)
		P[0][i] = A[i] - 'a';

	if (K == 1){
		printf("%d\n", N);
		return 0;
	}

	for (int i = 1; 1 << (i - 1) < N; ++i){
		for (int j = 0; j <= N; ++j)
			L[j].nr[0] = P[i - 1][j],
			L[j].nr[1] = j + (1 << (i - 1)) <= N ? P[i - 1][j + (1 << (i - 1))] : -1,
			L[j].p = j;

		sort(L, L + N, cmp);
		for (int j = 0, r = 1; j < N; ++j){
			if (j && !eq(j)) r = 1;
			else if (j) ++r;
			
			if (r >= K) maxx = 1 << i - 1 << 1;
			P[i][L[j].p] = j && eq(j) ? P[i][L[j - 1].p] : j;
		}
	}
	printf("%d\n", maxx);

	return 0;
}