Cod sursa(job #2184022)

Utilizator TooHappyMarchitan Teodor TooHappy Data 23 martie 2018 17:40:09
Problema Cel mai lung subsir comun Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.09 kb
#include <bits/stdc++.h>
using namespace std;
     
ifstream in("cmlsc.in");
ofstream out("cmlsc.out");

int DP[1025][1025], a[1025], b[1025];

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

    int n, m; in >> n >> m;

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

    for(int i = 1; i <= m; ++i) {
        for(int j = 1; j <= n; ++j) {
            if(a[j] == b[i]) {
                DP[i][j] = DP[i - 1][j - 1] + 1;
            } else {
                DP[i][j] = max(DP[i - 1][j], DP[i][j - 1]);
            }
        }
    }

    out << DP[m][n] << '\n';
    vector< int > sol;

    int i = m, j = n;
    while(i >= 1 && j >= 1) {
        if(a[j] == b[i]) {
            sol.push_back(a[j]);
            i--;
            j--;
        } else {
            if(DP[i - 1][j] <= DP[i][j - 1]) {
                j--;
            } else {
                i--;
            }
        }
    }

    reverse(sol.begin(), sol.end());

    for(auto x: sol) out << x << " ";

    in.close(); out.close();
     
    return 0;
}