Pagini recente » Cod sursa (job #1744013) | Cod sursa (job #269349) | Cod sursa (job #378073) | Cod sursa (job #901590) | Cod sursa (job #1236325)
#include<algorithm>
#include<bitset>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<deque>
#include<fstream>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<utility>
#include<vector>
using namespace std;
#define dbg(x) (cout<<#x<<" = "<<(x)<<'\n')
const string problemName = "substr";
const string inputFile = problemName + ".in";
const string outputFile = problemName + ".out";
typedef long long int lld;
typedef pair<int, int> PII;
const int INF = (1LL << 31) - 1;
const lld LINF = (1LL << 62) - 1;
const int NMAX = 100000 + 5;
int N, K, step, length, sol;
char S[NMAX];
int P[4 * NMAX][18];
int T[NMAX];
bool cmp(int A, int B) {
if(P[A][step] == P[B][step])
return (P[A + length][step] < P[B + length][step]);
return (P[A][step] < P[B][step]);
}
void suffix_array() {
int i, j;
for(i = 1; i <= N; i++) {
P[i][0] = S[i] - 'a' + 1;
T[i] = i;
}
for(step = 0; (1 << (step + 1)) <= N; step++) {
length = (1 << step);
sort(T + 1, T + N + 1, cmp);
for(i = 1; i <= N; i = j) {
for(j = i; j <= N; j++)
if(P[T[i]][step] == P[T[j]][step] && P[T[i] + length][step] == P[T[j] + length][step])
P[T[j]][step + 1] = i;
else
break;
}
}
}
int lcp(int x, int y) {
int i, ans = 0;
for(i = step; i >= 0 && x <= N && y <= N; i--) {
if(P[x][i] == P[y][i]) {
ans += (1 << i);
x += (1 << i);
y += (1 << i);
}
}
return ans;
}
int main() {
int Q, i, x, y;
#ifndef ONLINE_JUDGE
freopen(inputFile.c_str(), "r", stdin);
freopen(outputFile.c_str(), "w", stdout);
#endif
cin.sync_with_stdio(false);
cin >> N >> K;
cin >> (S + 1);
suffix_array();
for(i = 1; i <= N - K + 1; i++)
sol = max(sol, lcp(T[i], T[i + K - 1]));
printf("%d\n", sol);
return 0;
}