Pagini recente » Cod sursa (job #842816) | Cod sursa (job #2351312) | Cod sursa (job #3215742) | Cod sursa (job #2250) | Cod sursa (job #1053516)
#include <fstream>
#include <unordered_map>
#include <string>
using namespace std;
ifstream cin("substr.in");
ofstream cout("substr.out");
const int nmax = 20000;
const int mod = int(1e9) + 7;
const int A_ = 33;
int N, K;
string str;
int h[nmax];
unordered_map<int, int> H;
bool isGood(int L) {
H.clear();
int hash = 0;
int X = 1;
for (int i = 0; i < L; i++) {
hash = (1LL * hash * A_ + str[i]) % mod;
X = 1LL * X * A_ % mod;
}
H[hash] = 1;
for (int i = L; i < N; i++) {
hash = (1LL * hash * A_ % mod + str[i] - 1LL * str[i - L] * X % mod + mod) % mod;
if (++H[hash] >= K) {
return true;
}
}
return false;
}
int main()
{
cin >> N >> K;
cin.get();
getline(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;
}