Pagini recente » Cod sursa (job #22786) | Cod sursa (job #2380767) | Cod sursa (job #1498523) | Cod sursa (job #2218835) | Cod sursa (job #3260344)
#include <fstream>
#include <cstring>
#include <algorithm>
#define nmax 16400
using namespace std;
ifstream cin("substr.in");
ofstream cout("substr.out");
int n,k,p,a[15][nmax],t[nmax],sol;
string s;
struct sufix{
int nr[2],p;
}l[nmax];
int lcp(int x,int y){
int l=0;
if(x==y)
return n-x;
for(int k=p-1;k>=0&&x<n&&y<n;k--)
if(a[k][x]==a[k][y]){
x+=(1<<k);
y+=(1<<k);
l+=(1<<k);
}
return l;
}
bool cmp(const sufix& a,const sufix& b){
if(a.nr[0]==b.nr[0])
return a.nr[1]<b.nr[1];
return a.nr[0]<b.nr[0];
}
int main()
{
cin>>n>>k>>s;
for(int i=0;i<n;i++)
a[0][i]=s[i]-'0';
for(p=1;(1<<(p-1))<=n;p++){
for(int i=0;i<n;i++){
l[i].p=i;
l[i].nr[0]=a[p-1][i];
if(i+(1<<(p-1))<n)
l[i].nr[1]=a[p-1][i+(1<<(p-1))];
else
l[i].nr[1]=-1;
}
sort(l,l+n,cmp);
for(int i=0;i<n;i++)
if(i!=0&&l[i].nr[0]==l[i-1].nr[0]&&l[i].nr[1]==l[i-1].nr[1])
a[p][l[i].p]=a[p][l[i-1].p];
else
a[p][l[i].p]=i;
}
p--;
for(int i=0;i<n;i++)
t[a[p][i]]=i;
for(int i=0;i<n-k+1;i++)
sol=max(sol,lcp(t[i],t[i+k-1]));
cout<<sol;
return 0;
}