Pagini recente » Cod sursa (job #3146358) | Cod sursa (job #2639470) | Cod sursa (job #460785) | Cod sursa (job #1171017) | Cod sursa (job #1064923)
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
#include <tr1/unordered_map>
using namespace std;
#define N 17000
#define M1 1000000003
#define M2 2000003
char a[N];
int n, K;
int isOk(int len) {
tr1::unordered_map<int, int> H1, H2;
tr1::unordered_map<int, int>::iterator it, jt;
int i;
int nr1 = 0, nr2 = 0;
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) {
it = H1.find(h1);
jt = H2.find(h2);
if (it == H1.end()) {
H1.insert(make_pair(h1, 1));
nr1 = 1;
}
else {
++it->second;
nr1 = it->second;
}
if (jt == H2.end()) {
H2.insert(make_pair(h2, 1));
nr2 = 1;
}
else {
++jt->second;
nr2 = jt->second;
}
//H1[h1]++;
//H2[h2]++;
int nr = min(nr1, nr2);//min(H1[h1], H2[h2]);
//printf ("(l: %d r:%d) (h1: %d h2: %d) (%d %d)\n", i - len + 1,
// i, h1, h2, H1[h1], H2[h2]);
if (nr >= K)
return true;
h1 = ((h1 - (pw1 * a[i - len + 1])) % M1 + M1) % M1;
h2 = ((h2 - (pw2 * a[i - len + 1])) % M2 + M2) % M2;
}
}
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;// 1 + (n / K);
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);
}