Cod sursa(job #1579864)

Utilizator robx12lnLinca Robert robx12ln Data 25 ianuarie 2016 10:03:17
Problema Substr Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.4 kb
#include<fstream>
#include<algorithm>
#define DIM 16400
using namespace std;
ifstream fin("substr.in");
ofstream fout("substr.out");
struct str{
    int cod1;
    int cod2;
    int poz;
} L[DIM];
int P[15][DIM],exp,put,maxim,n,k;
char s[DIM];
int cmp( const str &a, const str &b ){
    return (a.cod1 == b.cod2) ? (a.cod2 < b.cod2) : (a.cod1 < b.cod1);
}
int lcp( int x,int y ){
    int k1,rez = 0;
    if( x == y ) return n - x;
    for( k1 = exp - 1; k1 >= 0 && x < n && y < n; k1--){
        if( P[k1][x] == P[k1][y] ){
            x   += (1<<k1);
            y   += (1<<k1);
            rez += (1<<k1);
        }
    }
    return rez;
}
int main(){
    fin >> n >> k;
    fin.get(s,DIM);
    for( int i = 0; i < n ;i++ ){
        P[0][i] = s[i] - 'a';
    }
    for( exp = 1, put  = 1; (put>>1) < n; exp++, (put <<= 1) ){
        for( int i = 0; i < n; i++ ){
            L[i].poz  = i;
            L[i].cod1 = P[exp-1][i];
            L[i].cod2 = ( i + put < n ) ? P[exp-1][i + put] : -1;
        }
        sort( L, L + n , cmp);
        for( int i = 0; i < n; i++ ){
            P[exp][L[i].poz] = ( i > 0 && L[i].cod1 == L[i-1].cod1 && L[i].cod2 == L[i-1].cod2 ) ? P[exp][L[i - 1].poz] : i;
        }
    }
    maxim = 0;
    for( int i = 0; i < n - k; i++ ){
        maxim = max( maxim, lcp( P[exp-1][i], P[exp-1][i + k] ) );
    }
    fout << maxim << "\n";
    return 0;
}