Pagini recente » Cod sursa (job #1268337) | Cod sursa (job #886926) | Cod sursa (job #8206) | Cod sursa (job #3126941) | Cod sursa (job #2766708)
#include <fstream>
#include <unordered_map>
using namespace std;
int baza = 131;
int MOD = 1e9 + 9;
int n, k;
string s;
int put[16401];
void read() {
ifstream f("substr.in");
f >> n >> k;
f >> s;
f.close();
}
int rez;
unordered_map<int, short> mp;
bool ok(int x) {
int i, hash = 0;
mp.clear();
for (i = 0; i < x; i++)
hash = (hash + 1LL * put[x - i - 1] * s[i]) % MOD;
mp[hash]++;
for (i = x; i < n; i++) {
hash = (((1LL * hash - 1LL * s[i - x] * put[x - 1] % MOD) * 1LL * baza) % MOD + s[i]) % MOD;
mp[hash]++;
}
for (auto it = mp.begin(); it != mp.end(); it++)
if (it->second >= k)
return 1;
return 0;
}
void solve() {
int i;
put[0] = 1;
for (i = 1; i <= n; i++)
put[i] = (1LL * put[i - 1] * baza) % MOD;
int st, dr, mij;
st = 1, dr = n - 1;
while (st <= dr) {
mij = (st + dr) / 2;
if (ok(mij)) {
rez = mij;
st = mij + 1;
}
else dr = mij - 1;
}
}
void output() {
ofstream g("substr.out");
g << rez;
g.close();
}
int main() {
read();
solve();
output();
return 0;
}