Pagini recente » Cod sursa (job #3208830) | Cod sursa (job #2740448) | Cod sursa (job #985757) | Cod sursa (job #87836) | Cod sursa (job #1053584)
#include <fstream>
#include <unordered_map>
#include <string>
#include <cstring>
#define mod 16385000
using namespace std;
ifstream cin("substr.in");
ofstream cout("substr.out");
int N, K;
char str[1 << 15];
bool isGood(int L) {
unordered_map<int, int> H;
int hash = 0;
int X = 1;
int A_ = 33;
for (int i = 0; i < L; i++) {
hash = ( hash * A_ + str[i]) % mod;
X = X * A_ % mod;
}
H[hash] = 1;
if (K == 1) return true;
for (int i = L; i < N; i++) {
hash = ( hash * A_ + str[i]) % mod;
hash = hash - str[i - L] * X % mod;
if (hash < 0) hash += mod;
auto it = H.find(hash);
if (it != H.end()) {
it->second++;
if (it->second >= K) {
return true;
}
} else {
H[hash] = 1;
}
}
return false;
}
int main()
{
cin >> N >> K;
cin >> str;
int ans = 0;
for (int step = 1 << 15; step > 0; step >>= 1) {
if (step + ans <= N && isGood(step + ans)) {
ans += step;
}
}
cout << ans;
return 0;
}