Cod sursa(job #2313877)

Utilizator robx12lnLinca Robert robx12ln Data 7 ianuarie 2019 16:12:02
Problema Substr Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.8 kb
#include<bits/stdc++.h>
using namespace std;
ifstream fin("substr.in");
ofstream fout("substr.out");

int N, K, P[20][16400], stp, pw, Pos[16400], Ans, Sol;
char S[16400];

struct str{
    int x, y, ind;

    bool operator < ( const str &aux ) const {
        if( x == aux.x )
            return y < aux.y;
        return x < aux.x;
    }

    bool operator == ( const str &aux ) const {
        if( x == aux.x && y == aux.y )
            return true;
        return false;
    }

} L[16400];

int main(){
    fin >> N >> K;
    fin >> S + 1;
    for( int i = 1; i <= N; i++ ){
        L[i].x = S[i];
        L[i].y = -1;
        L[i].ind = i;
    }

    if( K == 1 ){
        fout << N << "\n";
        return 0;
    }

    int Log = 0;
    while( (1<<Log) <= N )
        Log++;

    for( stp = 0; stp <= Log; stp++ ){
        sort( L + 1, L + N + 1 );

        P[stp][ L[1].ind ] = 1;
        for( int i = 2; i <= N; i++ )
            if( L[i] == L[i - 1] )
                P[stp][ L[i].ind ] = P[stp][ L[i - 1].ind ];
            else
                P[stp][ L[i].ind ] = P[stp][ L[i - 1].ind ] + 1;


        for( int i = 1; i <= N; i++ ){
            L[i].ind = i;
            L[i].x = P[stp][i];
            if( i + (1<<stp) <= N )
                L[i].y = P[stp][i +(1<<stp)];
            else
                L[i].y = -1;
        }

    }

    for( int i = 1; i <= N; i++ )
        Pos[ P[Log][i] ] = i;

    for( int i = 1; i <= N - K + 1; i++ ){
        int a = Pos[i], b = Pos[i + K - 1];
        Sol = 0;
        for( stp = Log; stp >= 0 && a <= N && b <= N; stp-- )
            if( P[stp][a] == P[stp][b] )
                a += (1<<stp), b += (1<<stp), Sol += (1<<stp);
        Ans = max( Ans, Sol );
    }
    fout << Ans << endl;
    return 0;
}