Pagini recente » Cod sursa (job #504544) | Cod sursa (job #2353800) | Cod sursa (job #103226) | Cod sursa (job #1337429) | Cod sursa (job #1072253)
#include <iostream>
#include <fstream>
#include <cstring>
#include <vector>
using namespace std;
ifstream fin("substr.in");
ofstream fout("substr.out");
#define nmax 16394
#define m1 2000003
#define m2 666013
#define b1 257
#define b2 259
int i, n, k;
int cnt;
char a[nmax];
int h1, h2;
int pw1, pw2;
struct nod {
int x;
int l;
int ap;
nod *next;
nod(){};
nod(int _x, int _l, int _ap){x = _x; l = _l; ap = _ap;};
};
nod *H1[m1 + 3];
nod *H2[m2 + 3];
inline bool Search(nod *H[], int h, int lg, int &t){
for (nod *it = H[h]; it; it = it->next) {
if (it->x == h && it->l == lg) {
++it->ap;
t = it->ap;
return 1;
}
}
return 0;
}
inline void add(nod *H[], int h, int lg, int &t) {
nod *p = new nod;
p->x = h;
p->l = lg;
p->ap = 1;
p->next = H[h];
H[h] = p;
t = 1;
}
inline bool Found(int L) {
int j, times1 = 0, times2 = 0;
h1 = h2 = 0;
pw1 = pw2 = 1;
for (j = 1; j < L; ++j)
pw1 = (pw1 * b1) % m1,
pw2 = (pw2 * b2) % m2;
for (j = 1; j <= n; ++j) {
h1 = (h1 * b1 + a[j]) % m1;
h2 = (h2 * b2 + a[j]) % m2;
if (j < L) continue;
if (!Search(H1, h1, L, times1))
add(H1, h1, L, times1);
if (!Search(H2, h2, L, times2))
add(H2, h2, L, times2);
if (min(times1, times2) >= k)
return 1;
h1 = ((h1 - (pw1 * a[j - L + 1])) % m1 + m1) % m1;
h2 = ((h2 - (pw2 * a[j - L + 1])) % m2 + m2) % m2;
}
return 0;
}
int main() {
fin >> n >> k;
fin >> (a + 1);
for (cnt = 1; (cnt << 1) <= n; cnt <<= 1);
for (i = 1; cnt; cnt >>= 1) {
if (i + cnt <= n && Found(i + cnt))
i += cnt;
}
fout << i << '\n';
return 0;
}