Pagini recente » Monitorul de evaluare | Rating Alin Rusu (ruali12) | Cod sursa (job #229863) | Statistici Poenaru Andrei (andrei.poenaru) | Cod sursa (job #882206)
Cod sursa(job #882206)
#include <algorithm>
#include <fstream>
using namespace std;
ifstream fin("substr.in");
ofstream fout("substr.out");
const int nmax= 16384;
const int lgmax= 16;//= log_2(nmax);
char ch[nmax+2];
int p[lgmax][nmax];
struct str{
int x, y, i;
};
str ord[nmax];
struct lex_cmp{
bool operator()(str x, str y){
if (x.x!=y.x){
return x.x<y.x;
}else{
return x.y<y.y;
}
}
};
int n, n2;
int lcp(int x, int y){
int sol= 0;
if (x==y){
return n-x;
}
for (int k= n2-1; k>=0&& x<n&& y<n; --k){
if (p[k][x]==p[k][y]){
x+= (1<<k); y+= (1<<k);
sol+= (1<<k);
}
}
return sol;
}
int main(){
int k;
fin>>n>>k;
fin.getline(ch, 2);
fin.getline(ch, nmax+2);
for (int i= 0; i<n; ++i){
p[0][i]= ch[i]-'a';
}
for (int i= 0; (1<<i)<=n; ++i){
for (int j= 0; j<n; ++j){
ord[j].x= p[i][j];
if (j+(1<<i)<n){
ord[j].y= p[i][j+(1<<i)];
}else{
ord[j].y= -1;
}
ord[j].i= j;
}
sort(ord, ord+n, lex_cmp());
p[i+1][ord[0].i]= 0;
for (int j= 1; j<n; ++j){
if (ord[j].x==ord[j-1].x&& ord[j].y== ord[j-1].y){
p[i+1][ord[j].i]= p[i+1][ord[j-1].i];
}else{
p[i+1][ord[j].i]= j;
}
}
}
int sol= 0;
for (n2= 0; (1<<n2)<=n; ++n2){
}
for (int i= 0; i<=n-k; ++i){
int aux= lcp(ord[i].i, ord[i+k-1].i);
if (sol<aux){
sol= aux;
}
}
fout<<sol<<"\n";
return 0;
}