Cod sursa(job #2674403)

Utilizator BogdanTicuTicu Bogdan Valeriu BogdanTicu Data 19 noiembrie 2020 01:48:42
Problema Substr Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.75 kb
#include <bits/stdc++.h>

using namespace std;

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

const int MOD=666013;
const int BAZA = 27;

long long powe[17005],hashes[17005];
unordered_map<long long,int> m;
char s[17005];
int main()
{
	int n,k;
	in>>n>>k;
	in>>(s+1);
	powe[0]=1;
	for(int i=1;i<=16500;i++)
		powe[i]=(1ll*powe[i-1]*BAZA);
	for(int i=n;i>=1;i--)
		hashes[i]=(hashes[i+1]*BAZA+s[i]);
	int st=0;
	int dr=n;
	int ans=0;
	while(st<=dr)
	{
		bool ok=0;
		int mid=(st+dr)/2;
		for(int i=1;i<n-mid+1;i++)
		{
			long long aux=hashes[i]-(hashes[i+mid]*powe[mid]);
			m[aux]++;
		}
		for(auto x:m)
		{
			if(x.second>=k)
			{
				ok=1;
				ans=mid;
				break;
			}
		}
		m.clear();
		if(ok==1) st=mid+1;
		else dr=mid-1;
	}
	out<<ans;
	return 0;
}