#include <algorithm>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
using
namespace
std;
const
int
BASE = 53;
const
int
MODULUS = 1e9 + 7;
const
int
MAX_LENGTH = 100000;
long
long
rolling_hash(
const
string& s,
int
len,
vector<
long
long
>& power,
vector<
long
long
>& hashes)
{
long
long
hash = 0;
for
(
int
i = 0; i < len; ++i) {
hash = (hash * BASE + s[i]) % MODULUS;
}
hashes[1] = hash;
for
(
int
i = len; i < s.size(); ++i) {
hash = ((hash - power[len - 1] * s[i - len])
% MODULUS
+ MODULUS)
% MODULUS;
hash = (hash * BASE + s[i]) % MODULUS;
hashes[i - len + 2] = hash;
}
return
hash;
}
int
binary_search(
const
string& s,
int
k)
{
int
low = 0, high = s.size();
int
ans = 0;
vector<
long
long
> power(MAX_LENGTH), hashes(MAX_LENGTH);
power[0] = 1;
for
(
int
i = 1; i < MAX_LENGTH; ++i) {
power[i] = (power[i - 1] * BASE) % MODULUS;
}
while
(low <= high) {
int
mid = (low + high) / 2;
rolling_hash(s, mid, power, hashes);
unordered_map<
long
long
,
int
> count;
for
(
int
i = 1; i <= s.size() - mid + 1; ++i) {
count[hashes[i]]++;
}
bool
found =
false
;
for
(
const
auto
& p : count) {
if
(p.second >= k) {
found =
true
;
break
;
}
}
if
(found) {
ans = mid;
low = mid + 1;
}
else
{
high = mid - 1;
}
}
return
ans;
}
int
main()
{
string s =
"abababcdabcd"
;
int
k = 2;
int
ans = binary_search(s, k);
cout << ans << endl;
return
0;
}