Pagini recente » Cod sursa (job #567609) | Cod sursa (job #203797) | Cod sursa (job #2759933) | Cod sursa (job #86544) | Cod sursa (job #3209504)
#include <iostream>
#include <fstream>
#include <unordered_map>
#define P 666013
using namespace std;
ifstream fin("substr.in");
ofstream fout("substr.out");
string s;
int n, k;
unordered_map <int, short> M;
int Cod(char ch)
{
if ('A' <= ch && ch <= 'Z') return ch - 'A';
if ('a' <= ch && ch <= 'z') return ch - 'a' + 26;
return ch - '0' + 52;
}
bool Check(int L)
{
int i, x, p;
M.clear();
x = 0;
p = 1;
for (i = 1; i < L; i++)
p = (p * 62) % P;
for (i = 0; i < L; i++)
x = (x * 62 + Cod(s[i])) % P;
M[x] = 1;
for (; i < n; i++)
{
x = (x - Cod(s[i - L]) * p) % P;
if (x < 0) x += P;
x = (x * 62 + Cod(s[i])) % P;
M[x]++;
if (M[x] == k) return 1;
}
return 0;
}
void Search()
{
int st, dr, mij, l;
st = 1; dr = n; l = 1;
while (st <= dr)
{
mij = (st + dr) / 2;
if (Check(mij))
{
l = mij;
st = mij + 1;
}
else
dr = mij - 1;
}
fout << l << "\n";
}
int main()
{
fin >> n >> k;
fin >> s;
Search();
fout.close();
return 0;
}