Pagini recente » Cod sursa (job #482080) | Cod sursa (job #382450) | Cod sursa (job #355932) | Cod sursa (job #2954536) | Cod sursa (job #1053544)
#include <fstream>
#include <map>
#include <string>
using namespace std;
ifstream cin("substr.in");
ofstream cout("substr.out");
const int mod = 16385000;
const int A_ = 33;
int N, K;
string str;
bool isGood(int L) {
map<int, int> H;
int hash = 0;
int X = 1;
for (int i = 0; i < L; i++) {
hash = (1LL * 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 = (1LL * hash * A_ + str[i]) % mod;
hash = (hash - 1LL * str[i - L] * X % mod + 1LL * mod) % mod;
H[hash]++;
if (H[hash] >= K) {
return true;
}
}
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;
}