Pagini recente » Cod sursa (job #2565807) | Cod sursa (job #2368029) | Cod sursa (job #2025470) | Cod sursa (job #2360814) | Cod sursa (job #2347549)
#include<fstream>
#include<algorithm>
#define x first.first
#define y first.second
#define z second
#define DIM 16400
using namespace std;
ifstream fin("substr.in");
ofstream fout("substr.out");
int n,k,h,lung,nr;
int a[17][16400],v[16400];
char s[16400];
pair< pair<int,int>,int> L[16400];
int LCP(int i,int j) {
int E=h;
int pow= 1<<h;
int sol=0;
while(pow!=0){
if(a[E][i]==a[E][j]) {
sol+=pow;
i+=pow;
j+=pow;
}
E--;pow/=2;
}
return sol;
}
int main(){
fin>>n>>k;
fin>>s;
for(int i=0;i<n;i++)
a[0][i]=s[i]-'0';
/// PRIMA LINIE
for(h=0,lung=1; lung<=n ; h++,lung*=2){
for(int i=0;i<n;i++){
L[i].x=a[h][i];
if(i+lung<n)
L[i].y=a[h][i+lung];
else
L[i].y=-1;
L[i].z=i;
}
sort(L,L+n);
a[h+1][L[0].z]=0;
for(int i=1;i<n;i++)
if(L[i].x==L[i-1].x && L[i].y==L[i-1].y)
a[h+1][L[i].z]=a[h+1][L[i-1].z];
else
a[h+1][L[i].z]=a[h+1][L[i-1].z]+1;
}
/// PREFIXE SORTATE
for(int i=0;i<n;i++)
v[a[h][i]]=i;
for(int i=0;i+k-1<n;i++) {
nr=max(nr, LCP(v[i],v[i+k-1]) );
}
fout<<nr;
return 0;
}