Pagini recente » Cod sursa (job #1156589) | Cod sursa (job #781638) | Cod sursa (job #1528705) | Cod sursa (job #462068) | Cod sursa (job #1520807)
#include <fstream>
using namespace std;
typedef unsigned long long u64;
const int MAX_N = 16384;
const int BASE = 107;
const int MOD = 196613;
const int NIL = -1;
struct cell {
u64 H;
int contor, next;
};
cell l[MAX_N];
int start[MOD];
int ptr;
u64 H[MAX_N];
u64 pow[MAX_N];
int hashAdd(const u64 &KEY) {
int i = start[KEY % MOD];
while ((i != NIL) && (l[i].H != KEY)) {
i = l[i].next;
}
if (i != NIL) {
return ++l[i].contor;
} else {
l[ptr] = { KEY, 1, start[KEY % MOD] };
start[KEY % MOD] = ptr++;
return 1;
}
}
int main(void) {
ifstream in("substr.in");
ofstream out("substr.out");
char s[MAX_N + 1];
int n, k;
in >> n >> k >> s;
H[0] = s[0];
pow[0] = 1ULL;
for (int i = 1; i < n; i++) {
H[i] = (H[i - 1] * BASE + s[i]);
pow[i] = (pow[i - 1] * BASE);
}
auto binSearch = [&] () -> int {
auto go = [&] (const int &LEN) -> int {
for (int i = 0; i < MOD; i++) {
start[i] = NIL;
}
ptr = 0;
int q = 0;
for (int i = 0; i + LEN <= n; i++) {
q = max(q, hashAdd(H[i + LEN - 1] - (i ? H[i - 1] * pow[LEN] : 0)));
}
return q;
};
int step = n, pos = 0;
while (step & (step - 1)) {
step -= (step & -step);
}
for (; step; step >>= 1) {
if ((pos + step < n) && go(pos + step) >= k) {
pos += step;
}
}
return pos;
};
out << binSearch() << '\n';
out.close();
return 0;
}