Pagini recente » Cod sursa (job #2954999) | Borderou de evaluare (job #586502) | Cod sursa (job #1499243) | Cod sursa (job #1053593)
#include <fstream>
#include <unordered_map>
#include <string>
#include <cstring>
using namespace std;
ifstream cin("substr.in");
ofstream cout("substr.out");
const int mod = 16385000;
int N, K;
char str[1 << 15];
bool isGood(int L) {
unordered_map<int, int> H;
int hash = 0;
int X = 1;
for (int i = 0; i < L; i++) {
hash = ((hash << 5) + hash + str[i]) % mod;
X = ((X << 5) + X) % mod;
}
H[hash] = 1;
if (K == 1) return true;
for (int i = L; i < N; i++) {
hash = ((hash << 5) + str[i]) % mod;
hash -= (X * str[i - L]) % mod;
if (hash < 0)
hash += mod;
if (++H[hash] >= K) {
return true;
}
}
return false;
}
int main()
{
cin >> N >> K >> str;
if (K == 1) {
cout << N;
return 0;
}
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;
}