Cod sursa(job #1053550)

Utilizator ELHoriaHoria Cretescu ELHoria Data 12 decembrie 2013 20:17:47
Problema Substr Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <fstream>
#include <unordered_map>
#include <string>

using namespace std;

ifstream cin("substr.in");
ofstream cout("substr.out");

const int mod = 16385000;
const int A_ = 33;
int N, K;
string str;

bool isGood(int L) {
	unordered_map<int, int> H;
	int hash = 0;
	int X = 1;
	for (int i = 0; i < L; i++) {
		hash = (1LL * hash * A_  + str[i]) % mod;
		X = X * A_ % mod;
	}
	
	H[hash] = 1;
	if (K == 1) return true;
	for (int i = L; i < N; i++) {
		hash = (1LL * hash * A_  + str[i]) % mod;
		hash = (hash - 1LL * str[i - L] * X  % mod + 1LL * mod) % mod;
		if (H.find(hash) != H.end()) {
			H[hash]++;
		} else {
			H[hash] = 1;
		}

		if (H[hash] >= K) {
			return true;
		}
	}
	return false;
}

int main()
{
	cin >> N >> K;
	cin >> str;
	int ans = 0;
	for (int step = 1 << 15; step > 0; step >>= 1) {
		if (step + ans <= N && isGood(step + ans)) {
			ans += step;
		}
	}
	cout << ans;
	return 0;
}