Pagini recente » Cod sursa (job #560782) | Borderou de evaluare (job #1504611) | Cod sursa (job #1174215) | Borderou de evaluare (job #2039905) | Cod sursa (job #1967197)
#include <iostream>
#include <fstream>
#include <cstdio>
#include <queue>
#include <vector>
#include <string.h>
#include <algorithm>
#include <limits.h>
#include <iomanip>
#include <cmath>
#define nMax 16390
#define log2Max 16
#define eulMax 64004
#define pb push_back
#define mkp make_pair
#define x first
#define y second
using namespace std;
ifstream fin("substr.in");
ofstream fout("substr.out");
int n, k, stp, cnt;
int P[log2Max][nMax], which[nMax];
pair<pair<int, int>, int> L[nMax];
char sir[nMax];
int query(int f, int s)
{
int rez=0;
if(f==s)
return n-f+1;
for(int k=stp; k>=0 && f<=n && s<=n; k--)
{
if(P[k][f]==P[k][s])
{
rez+=(1 << k);
f+=(1 << k);
s+=(1 << k);
}
}
return rez;
}
int main()
{
fin>>n>>k;
fin>>sir+1;
for(int i=1; i<=n; i++)
P[0][i]=sir[i]-'a';
for(stp=1, cnt=1; (cnt >> 1)<=n; stp++, cnt <<= 1)
{
for(int i=1; i<=n; i++)
{
L[i].x.x=P[stp-1][i];
L[i].x.y=(i+cnt<=n ? P[stp-1][i+cnt] : -1);
L[i].y=i;
}
sort(L+1, L+n+1);
for(int i=1; i<=n; i++)
P[stp][L[i].y]=(i>1 && L[i].x.x==L[i-1].x.x && L[i].x.y==L[i-1].x.y ? P[stp][L[i-1].y] : i);
}
stp--;
for(int i=1; i<=n; i++)
which[P[stp][i]]=i;
int Max=0;
for(int i=1; i+k-1<=n; i++)
Max=max(Max, query(which[i], which[i+k-1]));
fout<<Max;
}