Cod sursa(job #2478060)

Utilizator BogdanRazvanBogdan Razvan BogdanRazvan Data 21 octombrie 2019 16:37:06
Problema Cel mai lung subsir comun Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.86 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin ("cmlsc.in");
ofstream fout ("cmlsc.out");

const int N = 1025;

int a[N], b[N], d[N][N], ans[N];

int main()
{
    ios::sync_with_stdio(false);
    fin.tie(0);

    int n, m;

    fin >> n >> m;
    for(int i = 1; i <= n; ++i) fin >> a[i];
    for(int i = 1; i <= m; ++i) fin >> b[i];

    for(int i = 1; i <= n; ++i) {
        for(int j = 1; j <= m; ++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]);
        }
    }

    int i = 1, j = 1;
    while(i <= n && j <= m) {
        if(a[i] == b[j]) ans[++ans[0]] = a[i], ++i, ++j;
        else if(d[i + 1][j] >= d[i][j + 1]) ++i;
        else ++j;
    }

    fout << ans[0] << "\n";
    for(int i = 1; i <= ans[0]; ++i) fout << ans[i] << " ";
    return 0;
}