Pagini recente » Cod sursa (job #1873074) | Cod sursa (job #2355853) | Cod sursa (job #1769702) | Cod sursa (job #940134) | Cod sursa (job #454864)
Cod sursa(job #454864)
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAX_N = 17000;
struct suf
{
int nr[2], p;
} L[MAX_N];
int N, K, P[20][MAX_N], V[MAX_N], stp, Sol;
char S[MAX_N];
void citire()
{
scanf("%d %d\n", &N, &K);
fgets(S+1, MAX_N, stdin);
while(S[N] == '\n') --N;
}
struct cmp
{
bool operator()(const suf &a, const suf &b) const
{
if(a.nr[0] == b.nr[0])
return a.nr[1] < b.nr[1];
return a.nr[0] < b.nr[0];
}
};
int lcp(int x, int y)
{
if(x == y) return N - x + 1;
int sol = 0;
for(int k = stp - 1; k >= 0 && x <= N && y <= N; --k)
if(P[k][x] == P[k][y])
sol += (1 << k), x += (1 << k), y += (1 << k);
return sol;
}
void suffix()
{
for(int i = 1; i <= N; ++i)
P[0][i] = S[i];
int cnt = 1;
for(stp = 1; (cnt >> 1) < N; cnt <<= 1, ++stp)
{
for(int i = 1; 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+1, L+N+1, cmp());
for(int i = 1; i <= N; ++i)
if(i > 1 && L[i].nr[0] == L[i-1].nr[0] && L[i].nr[1] == L[i-1].nr[1])
P[stp][L[i].p] = P[stp][L[i-1].p];
else
P[stp][L[i].p] = i;
}
for(int i = 1; i+K-1 <= N; ++i)
Sol = max(Sol, lcp(L[i].p, L[i+K-1].p));
printf("%d\n", Sol);
}
int main()
{
freopen("substr.in","rt",stdin);
freopen("substr.out","wt",stdout);
citire();
suffix();
}