Pagini recente » Cod sursa (job #127417) | Cod sursa (job #1560022) | Cod sursa (job #49359) | Cod sursa (job #899228) | Cod sursa (job #2200166)
#include <bits/stdc++.h>
using namespace std;
#if 1
#define pv(x) cout<<#x<<" = "<<(x)<<"; ";cout.flush()
#define pn cout<<endl
#else
#define pv(x)
#define pn
#endif
#ifdef INFOARENA
ifstream in("substr.in");
ofstream out("substr.out");
#else
#define in cin
#define out cout
#endif
using ll = long long;
using ull = unsigned long long;
using ui = unsigned int;
#define pb push_back
#define mp make_pair
const int NMax = 2e4 + 5;
const ll inf_ll = 1e18 + 5;
const int inf_int = 1e9 + 5;
using zint = int;
int main() {
cin.sync_with_stdio(false);
cin.tie(0);
int N,K;
in >> N >> K;
string str;
in >> str;
int pw = 1;
for (; pw <= N; pw <<= 1) ;
pw >>= 1;
auto isGood = [&str, N, K] (int len) {
int hash1 = 0, put1 = 1;
int hash2 = 0, put2 = 1;
const int mod1 = 1e5 + 19, mod2 = 1e5 + 3, base = 73;
for (int i = 0; i < len; ++i) {
hash1 = (hash1 * base + (str[i]-'a') * base) % mod1;
hash2 = (hash2 * base + (str[i]-'a') * base) % mod2;
put1 = (put1 * base) % mod1;
put2 = (put2 * base) % mod2;
}
int mx = 0;
map<pair<int,int>, int> occ;
for (int i = len; i < N; ++i) {
hash1 = ((hash1 - ((str[i - len] - 'a') * put1 % mod1) + mod1) * base + (str[i] - 'a') * base) % mod1;
hash2 = ((hash2 - ((str[i - len] - 'a') * put2 % mod2) + mod2) * base + (str[i] - 'a') * base) % mod2;
pair<int,int> p = {hash1, hash2};
occ[p] += 1;
if (mx < occ[p]) {
mx = occ[p];
}
}
return mx >= K ? true : false;
};
int ans = 0;
while (pw) {
if (ans + pw <= N && isGood(ans + pw)) {
ans += pw;
}
pw >>= 1;
}
out << ans;
return 0;
}