Cod sursa(job #2594224)

Utilizator FlorusRuscuta Florin Florus Data 5 aprilie 2020 16:31:49
Problema Cel mai lung subsir comun Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.86 kb
#include <fstream>

using namespace std;
const int N = 1024;
ifstream fin("cmlsc.in");
ofstream fout("cmlsc.out");

int dp[N][N];
int len, n, m;
int sol[N], a[N], b[N];



int main()
{
    fin >> m >> n;
    for (int i = 1; i <= m; i++)
        fin >> a[i];
    for (int i = 1; i <= n; i++)
        fin >> b[i];
    for (int i = 1; i <= m; i++)
        for (int j = 1; j <= n; j++)
    if (a[i] == b[j])
        dp[i][j] = 1 + dp[i - 1][j - 1];
    else
        dp[i][j] = max(dp[i-1][j], dp[i][j - 1]);

    fout << dp[m][n] << "\n";

    for (int i = m; i >= 1;)
        for (int j = n; j >= 1;)
            if (a[i] == b[j])
                sol[++len] = a[i], i--, j--;

                else if (dp[i-1][j] < dp[i][j-1]) --j;

                    else --i;

    for (int i = len; i >= 1; i--)
        fout << sol[i] << " ";
    return 0;
}