Pagini recente » Monitorul de evaluare | Simulare clasa a 9-a (structuri) | Cod sursa (job #431175) | Istoria paginii runda/simulareg_1 | Cod sursa (job #2347743)
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin("substr.in");
ofstream fout("substr.out");
int v[20000],h,p[20][20000],n,k,sol,lg;
char s[20000];
pair< pair <int ,int> ,int >a[20000];
int cautbinar(int i,int j){
int putere=(1<<h);
int exponent=h;
int sol=0;
while(putere){
if(p[exponent][i]==p[exponent][j]){
sol+=putere;
i+=putere;
j+=putere;
}
putere=putere/2;
exponent--;
}
return sol;
}
int main(){
fin>>n>>k;
if(k==1){
fout<<n;
return 0;
}
fin>>s;
for(int i=0;s[i]!=0;i++){
p[0][i]=s[i]-'0';
}
for(h=0,lg=1;lg<=n;h++,lg*=2){
for(int i=0;i<n;i++){
a[i].first.first=p[h][i];
if(i+lg<n){
a[i].first.second=p[h][i+lg];
}
else{
a[i].first.second=-1;
}
a[i].second=i;
}
sort(a,a+n);
p[h+1][a[0].second]=0;
for(int i=1;i<n;i++){
if(a[i].first.first==a[i-1].first.first && a[i].first.second==a[i-1].first.second){
p[h+1][a[i].second]=p[h+1][a[i-1].second];
}
else{
p[h+1][a[i].second]=p[h+1][a[i-1].second]+1;
}
}
}
for(int i=0;i<n;i++){
v[p[h][i]]=i;
}
sol=0;
for(int i=0;i+k-1<n;i++){
sol=max(sol,cautbinar(v[i],v[i+k-1]));
}
fout<<sol;
}