Cod sursa(job #2051156)

Utilizator giotoPopescu Ioan gioto Data 28 octombrie 2017 16:43:00
Problema Seif Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.66 kb
#include <bits/stdc++.h>
using namespace std;

int n, m, t, k;
int d[1005][1005], p[2][28], next[2][1005];
char a[1005], b[1005], sol[1005];
char s[1005];
int main()
{
    freopen("seif.in", "r", stdin);
    freopen("seif.out", "w", stdout);
    scanf("%d", &t);
    while(t--){
        scanf("%d", &k);
        scanf("%s", a + 1);
        scanf("%s", b + 1);
        int L1 = strlen(a + 1), L2 = strlen(b + 1);
        memset(d, 0, sizeof(d));
        for(int i = L1; i >= 1 ; --i){
            for(int j = L2; j >= 1 ; --j){
                if(a[i] == b[j]) d[i][j] = d[i + 1][j + 1] + 1;
                else d[i][j] = max(d[i + 1][j], d[i][j + 1]);
            }
        }
        for(int i = 0; i <= 25 ; ++i) p[0][i] = p[1][i] = 1002;
        for(int i = L1; i >= 1 ; --i){
            next[0][i] = p[0][a[i] - 'A'];
            p[0][a[i] - 'A'] = i;
        }
        for(int i = L2; i >= 1 ; --i){
            next[1][i] = p[1][a[i] - 'A'];
            p[1][b[i] - 'A'] = i;
        }
        int i = 1, j = 1;
        bool ok = 0;
        while(!ok){
            char c = 'Z' - 'A';
            ok = 1;
            while(1 == 1){
                while(p[0][c] < i) p[0][c] = next[0][p[0][c]];
                while(p[1][c] < j) p[1][c] = next[1][p[1][c]];
                if(d[p[0][c]][p[1][c]] >= k && p[0][c] <= L1 && p[1][c] <= L2){
                    printf("%c", c + 'A'); --k;
                    i = p[0][c] + 1; j = p[1][c] + 1;
                    ok = 0;
                    break;
                }
                --c;
            }
            if(i > L1 || j > L2) break;
        }
        printf("\n");
    }
    return 0;
}