Pagini recente » Rating Rus Cristian (JHarciog) | Cod sursa (job #1521381) | Cod sursa (job #1321720) | Cod sursa (job #1996900) | Cod sursa (job #2949281)
#include <iostream>
#include <fstream>
#include <unordered_map>
using namespace std;
ifstream fin("substr.in");
ofstream fout("substr.out");
const int MOD = 1e9 + 7;
const int BASE = 29;
int n, k;
string s;
unordered_map <int, int> umap;
bool check(int sz){
umap.clear();
int powB = 1;
for(int i = 1; i <= sz; i++)
powB = (1ll * powB * BASE) % MOD;
int hash = 0;
for(int i = 1; i <= n; i++){
hash = (1ll * hash * BASE + (s[i] - 'a')) % MOD;
if(i >= sz){
hash = (1ll * hash + MOD - ((1ll * powB * (s[i - sz] - 'a')) % MOD)) % MOD;
if(umap[hash] + 1 >= k)
return true;
umap[hash]++;
}
}
return false;
}
int main(){
fin >> n >> k;
fin >> s, s = "$" + s;
s[0] = 'a';
int st = 1, dr = n, ans = 0;
while(st <= dr){
int mij = (st + dr) / 2;
if(check(mij))
st = mij + 1, ans = mij;
else
dr = mij - 1;
}
fout << ans << '\n';
return 0;
}