Pagini recente » Cod sursa (job #1973540) | Cod sursa (job #1387428) | Cod sursa (job #1760153) | Cod sursa (job #1429424) | Cod sursa (job #1065360)
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
#include <vector>
using namespace std;
#define N 17000
#define M1 666013
#define M2 2000003
char a[N];
int n, K;
int H1[M1], H2[M2];
vector<int> remove_H1, remove_H2;
// remove only the necessary hash values
void remove() {
for (int i = 0; i < remove_H1.size(); ++i)
H1[ remove_H1[i] ] = 0;
for (int i = 0; i < remove_H2.size(); ++i)
H2[ remove_H2[i] ] = 0;
}
int isOk(int len) {
//map<int, int> H1, H2;
int i;
int h1 = 0, h2 = 0;
int pw1 = 1, pw2 = 1;
for (i = 1; i < len; ++i)
pw1 = (pw1 * 257) % M1,
pw2 = (pw2 * 259) % M2;
for (i = 1; i <= n; ++i) {
h1 = (h1 * 257 + a[i]) % M1;
h2 = (h2 * 259 + a[i]) % M2;
if (i >= len) {
H1[h1]++;
H2[h2]++;
remove_H1.push_back(h1);
remove_H2.push_back(h2);
int nr = min(H1[h1], H2[h2]);
if (nr >= K) {
remove();
return true;
}
h1 = ((h1 - (pw1 * a[i - len + 1]) % M1) % M1 + M1) % M1;
h2 = ((h2 - (pw2 * a[i - len + 1]) % M2) % M2 + M2) % M2;
}
}
remove();
return false;
}
int main() {
freopen ("substr.in", "r", stdin);
freopen ("substr.out", "w", stdout);
scanf ("%d %d\n", &n, &K);
scanf ("%s", a + 1);
int i, cnt;
int nr = n;
for (cnt = 1; cnt * 2 <= nr; cnt *= 2);
for (i = 1; cnt; cnt >>= 1)
if (i + cnt <= n && isOk(i + cnt))
i += cnt;
printf ("%d\n", i);
}