Pagini recente » Cod sursa (job #2223114) | Cod sursa (job #961736) | Cod sursa (job #1447046) | Cod sursa (job #2262487) | Cod sursa (job #1574672)
#include <fstream>
#include <cstring>
#include <algorithm>
#define MAXN 17000
#define MAXLG 20
using namespace std;
ifstream fin("substr.in");
ofstream fout("substr.out");
char A[MAXN];
struct entry{
int nr[2],p;
} L[MAXN];
int P[MAXN][MAXLG], N, i ,stp ,cnt, K;
int Sol;
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]);
}
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(){
fin >> N >> K;
fin >> A;
for(int 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;
}
Sol = -1;
for(int i=0;i<N-K;i++)
Sol = max(Sol,lcp(L[i].p,L[i+K-1].p));
fout << Sol << "\n";
}