Cod sursa(job #2003918)

Utilizator robx12lnLinca Robert robx12ln Data 24 iulie 2017 13:19:37
Problema Seif Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.06 kb
#include<fstream>
#include<cstring>
#define DIM 1005
using namespace std;
ifstream fin("seif.in");
ofstream fout("seif.out");
int next1[DIM][30], next2[DIM][30], D[DIM][DIM];
char a[DIM], b[DIM];
int n, m, k, t;
int main(){
    for( fin >> t; t != 0; t-- ){
        fin >> k;
        fin >> a + 1 >> b + 1;
        n = strlen(a + 1);
        m = strlen(b + 1);
        for( int i = n; i >= 1; i-- ){
            for( int j = m; j >= 1; j-- ){
                if( a[i] == b[j] ){
                    D[i][j] = 1 + D[i + 1][j + 1];
                }else{
                    D[i][j] = max( D[i + 1][j], D[i][j + 1] );
                }
            }
        }
        for( int ch = 0; ch <= (int)('Z' - 'A'); ch++ ){
            for( int i = n; i >= 1; i-- ){
                if( a[i] == (char)(ch + 'A') ){
                    next1[i][ch] = i;
                }else{
                    next1[i][ch] = next1[i + 1][ch];
                }
            }
        }
        for( int ch = 0; ch <= (int)('Z' - 'A'); ch++ ){
            for( int i = m; i >= 1; i-- ){
                if( b[i] == (char)(ch + 'A') ){
                    next2[i][ch] = i;
                }else{
                    next2[i][ch] = next1[i + 1][ch];
                }
            }
        }
        int x = 1;
        int y = 1;
        int ok = 1;
        while( ok == 1 ){
            ok = 0;
            for( int ch = (int)('Z' - 'A'); ch >= 0; ch-- ){
                if( next1[x][ch] != 0 && next2[y][ch] != 0 && D[ next1[x][ch] ][ next2[y][ch] ] >= k ){
                    k = (k == 0) ? 0 : k - 1;
                    x = next1[x][ch] + 1;
                    y = next2[y][ch] + 1;
                    ok = 1;
                    fout << (char)(ch + 'A');
                    break;
                }
            }
        }
        memset( D, 0, sizeof(D) );
        memset( a, 0, sizeof(a) );
        memset( b, 0, sizeof(b) );
        memset( next1, 0, sizeof(next1) );
        memset( next2, 0, sizeof(next2) );
        fout << "\n";
    }
    return 0;
}