Cod sursa(job #2343200)

Utilizator Raresr14Rosca Rares Raresr14 Data 13 februarie 2019 19:33:57
Problema Substr Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.38 kb
#include <fstream>
#include <algorithm>
#define DIM 16400
#define X first.first
#define Y first.second
#define poz second
using namespace std;
ifstream fin("substr.in");
ofstream fout("substr.out");
int n,k,i,e,l,a[20][DIM],v[DIM],sol;
char s[DIM];
pair<pair<int,int> ,int> L[DIM];
int lcp(int x, int y){
    int s=0;
    int p=(1<<e);
    int e1=e;
    while(p){
        if(a[e1][x]==a[e1][y]){
            x+=p;
            y+=p;
            s+=p;
        }
        e1--;
        p/=2;
    }
    return s;
}
int main(){
    fin>>n>>k;
    if(k==1){
        fout<<n;
        return 0;
    }
    fin>>s;
    for(i=0;i<n;i++)
        a[0][i]=s[i]-'0';
    for(e=0,l=1;l<=n;l*=2,e++){
        for(i=0;i<n;i++){
            if(i+l>=n){
                L[i].X=a[e][i];
                L[i].Y=-1;
                L[i].poz=i;
            }else{
                L[i].X=a[e][i];
                L[i].Y=a[e][i+l];
                L[i].poz=i;
            }
        }
        sort(L,L+n);
        a[e+1][L[0].poz]=0;
        for(i=1;i<n;i++)
            if(L[i].X==L[i-1].X&&L[i].Y==L[i-1].Y)
                a[e+1][L[i].poz]=a[e+1][L[i-1].poz];
            else
                a[e+1][L[i].poz]=a[e+1][L[i-1].poz]+1;
    }
    for(i=0;i<n;i++)
        v[a[e][i]]=i;
    for(i=0;i+k-1<n;i++)
        sol=max(sol,lcp(v[i],v[i+k-1]));
    fout<<sol;
    return 0;
}