Pagini recente » Cod sursa (job #76514) | Cod sursa (job #996246) | Cod sursa (job #287312) | Cod sursa (job #2628730) | Cod sursa (job #1468990)
#include <cstdio>
#include <algorithm>
#include <utility>
using namespace std;
const int N = 16390, inf = (1ll << 31) - 1;
struct CODE{
int c1, c2, ind;
};
char s [N];
CODE cod [N];
int n, lim, poz [15][N];
pair <int, int> c [N];
class MyComp {
public :
bool operator () (const CODE &A, const CODE &B) {
return A.c1 < B.c1 || (A.c1 == B.c1 && A.c2 < B.c2);
}
};
class MyComp2 {
public :
bool operator () (const pair <int, int> &A, const pair <int, int> &B) {
return A.first < B.first;
}
};
void suffixarray () {
int i, j, l;
for (i = 0; i < n; i ++)
poz [0][i] = s [i] - '0';
for (j = 1; (1 << j) < n; ++ j) {
lim = j;
l = (1 << (j - 1));
for (i = 0; i < n; i ++) {
cod [i].c1 = poz [j - 1][i];
if (i + l + l - 1 < n)
cod [i].c2 = poz [j - 1][i + l];
else cod [i].c2 = -1;
cod [i].ind = i;
}
sort (cod, cod + n, MyComp ());
for (i = 0; i < n; i ++)
if (i > 0 && cod [i].c1 == cod [i - 1].c1 && cod [i].c2 == cod [i - 1].c2)
poz [j][cod [i].ind] = poz [j][cod [i - 1].ind];
else
poz [j][cod [i].ind] = i;
}
}
int lcp (int x, int y) {
int j, step, l = 0;
if (x == y)
return n - x;
for (j = lim; j >= 0 && x < n && y < n; j --)
if (poz [j][x] == poz [j][y]) {
x = x + (1 << j);
y = y + (1 << j);
l = l + (1 << j);
}
return l;
}
int main () {
int k, i, ans = 0;
freopen ("substr.in", "r", stdin);
freopen ("substr.out", "w", stdout);
scanf ("%d%d", &n, &k);
scanf ("%s", s);
suffixarray ();
for (i = 0; i < n; i ++) {
c [i].first = poz [lim][i];
c [i].second = i;
}
sort (c, c + n, MyComp2 ());
for (i = 0; i < n; i ++) {
if (i + k - 1 < n)
ans = max (ans, lcp (c [i].second, c [i + k - 1].second));
}
printf ("%d\n", ans);
return 0;
}