Pagini recente » Cod sursa (job #963467) | Cod sursa (job #1307367) | Cod sursa (job #539714) | Cod sursa (job #352949) | Cod sursa (job #1520459)
#include <bits/stdc++.h>
using namespace std;
const unsigned base = 1000000007, mmr = 16385;
vector<int>Pow(mmr), Hash(mmr);
int N, M;
char S[mmr];
void dohash() {
Pow[0] = 1;
for(int i = 1; i < mmr; ++i)
Pow[i] = Pow[i - 1] * base;
for(int i = 1; i <= N; ++i)
Hash[i] = Hash[i - 1] * base + S[i];
}
unsigned h(int x, int y) {
return Hash[y] - Hash[x - 1] * Pow[y - x + 1];
}
bool able(const int &val) {
unordered_map<unsigned, int> freq;
for(int i = val; i <= N; ++i) {
int wh = h(i - val + 1, i);
++freq[wh];
if(freq[wh] >= M)
return 1;
}
return 0;
}
int main() {
ifstream cin("substr.in");
ofstream cout("substr.out");
cin >> N >> M >> S + 1;
dohash();
int ans = 0;
for(int step = 16384; step; step /= 2)
if(step + ans <= N && able(step + ans))
ans += step;
cout << ans;
return 0;
}