Pagini recente » Cod sursa (job #1873754) | Cod sursa (job #1347843) | Cod sursa (job #264295) | Cod sursa (job #2587514) | Cod sursa (job #1301978)
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream F("substr.in");
ofstream G("substr.out");
const int N = 100010;
int n,k,dp[20][N],ln,p[N];
char a[N];
struct trio { int a,b,p; } t[N];
bool cmpt(trio a,trio b)
{
return a.a < b.a || ( a.a == b.a && a.b < b.b );
}
bool cmp(int x,int y)
{
return dp[ln][x] < dp[ln][y];
}
void build()
{
for (int i=1;i<=n;++i)
dp[0][i] = a[i]-'0';
for (int i=1;(1<<i)<=n*2;++i)
{
for (int j=1;j<=n;++j)
{
t[j].a = dp[i-1][j];
t[j].b = j+(1<<i)/2 <= n ? dp[i-1][j+(1<<i)/2] : 0;
t[j].p = j;
}
sort(t+1,t+n+1,cmpt);
for (int j=1;j<=n;++j)
{
dp[i][t[j].p] = j;
if ( t[j].a == t[j-1].a && t[j].b == t[j-1].b )
dp[i][t[j].p] = dp[i][t[j-1].p];
}
ln = max(ln,i);
}
}
int lcp(int x,int y)
{
int ans = 0;
for (int i=ln;i>0 && x<=n && y<=n;--i)
if ( dp[i][x] == dp[i][y] )
{
ans += 1<<i;
x += 1<<i;
y += 1<<i;
}
return ans;
}
int main()
{
F>>n>>k;
F>>(a+1);
build();
for (int i=1;i<=n;++i)
p[i] = i;
sort(p+1,p+n+1,cmp);
int ans = 0;
for (int i=k,j=1;i<=n;++i,++j)
ans = max(ans,lcp(p[j],p[i]));
G<<ans<<'\n';
}