Pagini recente » Cod sursa (job #562219) | Cod sursa (job #1923737) | Cod sursa (job #1722512) | Cod sursa (job #1510472) | Cod sursa (job #2790614)
#include <bits/stdc++.h>
#define P ((int)1e9+7)
using namespace std;
ifstream fin("substr.in");
ofstream fout("substr.out");
unordered_map<int, int> M;
char text[16400];
int n, k, p62[16385];
void Make62()
{
p62[0] = 1;
for (int i = 1; i <= 16384; i++)
p62[i] = 1LL * p62[i - 1] * 62 % P;
}
int Cod(char c)
{
if (c <= '9') return c - '0';
if (c <= 'Z') return c - 'A' + 10;
return c - 'a' + 36;
}
int Verifica(int L)
{
M.clear();
int i, x = 0, e;
e = p62[L - 1];
for (i = 0; i < L; i++)
x = (1LL * x * 62 + Cod(text[i])) % P;
M[x]++;
for ( ; text[i] != 0; i++)
{
x = (1LL * (x - 1LL * Cod(text[i-L]) * e % P + P) * 62 % P + Cod(text[i])) % P;
M[x]++;
if (M[x] >= k) return 1;
}
return 0;
}
int CautBin()
{
int st, dr, L, mid;
st = 1; L = dr = n - k + 1;
while (st <= dr)
{
mid = (st + dr) / 2;
if (Verifica(mid) == 1)
{
L = mid;
st = mid + 1;
}
else dr = mid - 1;
}
return L;
}
int main()
{
fin >> n >> k;
fin >> text;
Make62();
fout << CautBin();
fout.close();
return 0;
}