Cod sursa(job #1575183)

Utilizator alexandra_udristoiuUdristoiu Alexandra Maria alexandra_udristoiu Data 21 ianuarie 2016 10:49:39
Problema Substr Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.5 kb
#include<fstream>
#include<algorithm>
#define DIM 16500
using namespace std;
int n, k, i, j, st, maxim, p;
int a[15][DIM];
struct suf{
    int ii;
    int jj;
    int poz;
};
suf w[DIM];
char s[DIM];
ifstream fin("substr.in");
ofstream fout("substr.out");
int cmp(suf a, suf b){
    if(a.ii == b.ii){
        return a.jj < b.jj;
    }
    return a.ii < b.ii;
}
int f(int x, int y){
    int sol = 0, i;
    for(i = st - 2; i >= 0; i--){
        if(a[i][x] == a[i][y]){
            x += (1 << i);
            y += (1 << i);
            sol += (1 << i);
        }
    }
    return sol;
}
int main(){
    fin>> n >> k;
    fin>> s + 1;
    for(i = 1; i <= n; i++){
        a[0][i] = s[i];
    }
    for(st = 1; (1 << (st - 1) ) < n; st++){
        p = (1 << (st - 1) );
        for(j = 1; j <= n; j++){
            w[j].poz = j;
            w[j].ii = a[st - 1][j];
            if(j + p <= n){
                w[j].jj = a[st - 1][j + p];
            }
            else{
                w[j].jj = -1;
            }
        }
        sort(w + 1, w + n + 1, cmp);
        for(j = 1; j <= n; j++){
            if(j != 1 && w[j - 1].ii == w[j].ii && w[j - 1].jj == w[j].jj){
                a[st][ w[j].poz ] = a[st][ w[j - 1].poz ];
            }
            else{
                a[st][ w[j].poz ] = j;
            }
        }
    }
    for(i = 1; i <= n - k + 1; i++){
        maxim = max(maxim, f(w[i].poz, w[i + k - 1].poz));
    }
    fout<< maxim <<"\n";
    return 0;
}