Cod sursa(job #2491428)

Utilizator Ionut28Porumb Palincas Ionut Ionut28 Data 12 noiembrie 2019 16:32:17
Problema Seif Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.97 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("seif.in");
ofstream fout("seif.out");
const int nmax = 1000;
string a, b;
int dp[nmax + 5][nmax + 5], nexta[nmax + 5][nmax + 5], nextb[nmax + 5][nmax + 5], t, k;
void precalc()
{
    for(int i = 1; i <= nmax; ++i)
        for(int j = 1; j <= nmax; ++j)
            dp[i][j] = 0;
    for(char ch = 'Z'; ch >= 'A'; --ch)
    {
        nexta[a.size() + 1][ch - 'A'] = -1;
        nextb[b.size() + 1][ch - 'A'] = -1;
        for(int i = a.size(); i >= 1; i--)
        {
            if(a[i - 1] == ch)
                nexta[i][ch - 'A'] = i;
            else
                nexta[i][ch - 'A'] = nexta[i + 1][ch - 'A'];
        }
        for(int i = b.size(); i >= 1; i--)
        {
            if(b[i - 1] == ch)
                nextb[i][ch - 'A'] = i;
            else
                nextb[i][ch - 'A'] = nextb[i + 1][ch - 'A'];
        }

    }
}
void dinamica()
{
    for(int i = a.size(); i >= 1; i--)
        for(int j = b.size(); j >= 1; j--)
            if(a[i - 1] == b[j - 1])
                dp[i][j] = 1 + dp[i + 1][j + 1];
            else
                dp[i][j] = max(dp[i + 1][j], dp[i][j + 1]);
}
void solve()
{
    bool ok = false;
    int contor = 0;
    int c1 = 1, c2 = 1;
    while(!ok)
    {
        ok = true;
        for(char ch = 'Z'; ch >= 'A'; --ch)
        {
            int poz1 = nexta[c1][ch - 'A'];
            int poz2 = nextb[c2][ch - 'A'];
            if(poz1 == -1 || poz2 == -1)
                continue;
            if(dp[poz1][poz2] >= k - contor)
            {
                contor++;
                ok = false;
                fout << ch;
                c1 = poz1 + 1;
                c2 = poz2 + 1;
            }
        }
    }
    fout << "\n";
}
int main()
{
    fin >> t;
    while(t--)
    {
        fin >> k;
        fin >> a;
        fin >> b;
        precalc();
        dinamica();
        solve();
    }
    return 0;
}