Pagini recente » Cod sursa (job #1014179) | Cod sursa (job #2278477) | Cod sursa (job #3179659) | Cod sursa (job #1161185) | Cod sursa (job #2674402)
#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<=17000;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;
}
}
m.clear();
if(ok==1) st=mid+1;
else dr=mid-1;
}
out<<ans;
return 0;
}