Pagini recente » Cod sursa (job #1211884) | Cod sursa (job #2547528) | Cod sursa (job #386921) | Cod sursa (job #1908890) | Cod sursa (job #1295759)
#include <iostream>
#include <tr1/unordered_map>
#include <string>
#include <fstream>
using namespace std;
typedef unsigned long long ull;
const int MAX_N = 16384 + 1;
int n, k;
tr1::unordered_map<ull, int> H;
char s[MAX_N];
const int PWR = 137;
ull p[MAX_N];
bool good(const int len) {
H.clear();
ull hash = 0;
int ret = 1;
for(int i = 0 ; i < len ; ++i) {
hash = hash * PWR + s[i];
}
H[hash]++;
for(int i = len ; i < n ; ++i) {
hash -= s[i - len] * p[len - 1];
hash = hash * PWR + s[i];
H[hash]++;
if(H[hash] > ret)
ret = H[hash];
}
return ret >= k;
}
int bsearch() {
int i, step;
for(step = 1 ; step < n ; step <<= 1);
for(i = 0 ; step ; step >>= 1) {
if(i + step <= n && good(i + step))
i += step;
}
return i;
}
int main() {
ifstream in("substr.in");
in >> n >> k;
in >> s;
in.close();
p[0] = 1;
for(int i = 1 ; i <= n ; ++i)
p[i] = p[i-1] * PWR;
ofstream out("substr.out");
out << bsearch() << "\n";
out.close();
}