Pagini recente » Cod sursa (job #1499799) | Cod sursa (job #1812325) | Cod sursa (job #2460422) | Cod sursa (job #1361757) | Cod sursa (job #2385439)
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 65536;
const int MAXLG = 17;
char A[MAXN];
struct entry {
int nr[2], p;
} L[MAXN];
int P[MAXLG][MAXN], N, i, stp, cnt, q;
bool cmp(const entry &a, const entry &b) {
return a.nr[0] == b.nr[0] ? (a.nr[1] < b.nr[1]) : (a.nr[0] < b.nr[0]);
}
void print_p() {
for(int k = 0; k < stp; ++k) {
for(int i = 0; i < N; ++i) {
printf("%d ", P[k][i]);
}
printf("\n");
}
printf("\n");
}
int lcp(int x, int y) {
int k, ret = 0;
if (x == y) return N - x;
for (k = stp - 1; k >= 0 && x < N && y < N; --k)
if (P[k][x] == P[k][y])
x += 1 << k, y += 1 << k, ret += 1 << k;
return ret;
}
int main() {
FILE *in = fopen("substr.in", "r");
fscanf(in, "%d %d\n", &N, &q);
fgets(A, MAXN, in);
fclose(in);
for (i = 0; i < N; ++i)
P[0][i] = A[i] - 'a';
for (stp = 1, cnt = 1; cnt >> 1 < N; ++stp, cnt <<= 1) {
for (i = 0; i < N; ++i) {
L[i].nr[0] = P[stp - 1][i];
L[i].nr[1] = i + cnt < N ? P[stp - 1][i + cnt] : -1;
L[i].p = i;
}
sort(L, L + N, cmp);
for (i = 0; i < N; ++i)
P[stp][L[i].p] = i > 0 && L[i].nr[0] == L[i - 1].nr[0] && L[i].nr[1] == L[i - 1].nr[1] ? P[stp][L[i - 1].p] : i;
}
//print_p();
int max = 0;
for(int i = 0; i < N - q; ++i) {
int prefix = lcp(i, i + q - 1);
//printf("%d %d: %d\n", i, i + q - 1, prefix);
if(prefix > max) {
max = prefix;
}
}
FILE *out = fopen("substr.out", "w");
fprintf(out, "%d\n", max + 1);
fclose(out);
return 0;
}