Pagini recente » Cod sursa (job #806393) | Cod sursa (job #432155) | Cod sursa (job #1426116) | Cod sursa (job #3210855) | Cod sursa (job #2922013)
#include <bits/stdc++.h>
#define BASE 26
#define MOD1 666013
#define MOD2 666019
using namespace std;
ifstream fin ("substr.in");
ofstream fout ("substr.out");
int n, k;
string s;
map <pair<int, int>, int> mp;
int test(int len) {
int i, hash1, hash2, power1, power2, maxap;
hash1 = hash2 = 0;
power1 = power2 = 1;
for (i = 0; i < len - 1; i++) {
hash1 = (hash1 * BASE + s[i] - 'a') % MOD1;
hash2 = (hash2 * BASE + s[i] - 'a') % MOD2;
power1 = (power1 * BASE) % MOD1;
power2 = (power2 * BASE) % MOD2;
}
maxap = 1;
mp.clear();
for (i = len - 1; i < n; i++) {
hash1 = (hash1 * BASE + s[i] - 'a') % MOD1;
hash2 = (hash2 * BASE + s[i] - 'a') % MOD2;
auto it = mp.find({hash1, hash2});
if (it != mp.end()) {
it->second++;
maxap = max(maxap, it->second);
}
else
mp.insert({{hash1, hash2}, 1});
hash1 = (hash1 - (power1 * (s[i - len + 1] - 'a')) % MOD1 + MOD1) % MOD1;
hash2 = (hash2 - (power2 * (s[i - len + 1] - 'a')) % MOD2 + MOD2) % MOD2;
}
return maxap;
}
int bs(int st, int dr) {
int mij, sol, val;
while (st <= dr) {
mij = (st + dr) / 2;
val = test(mij);
if (val >= k) {
st = mij + 1;
sol = mij;
}
else
dr = mij - 1;
}
return sol;
}
int main() {
fin >> n >> k >> s;
fout << bs(1, n);
return 0;
}