Cod sursa(job #2649205)

Utilizator Katherine456719Swan Katherine Katherine456719 Data 13 septembrie 2020 14:42:57
Problema Cel mai lung subsir comun Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.66 kb
#include <bits/stdc++.h>

using namespace std;

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

int dp[1025][1025];
int a[1025] ,b[1025];
set <int> ans;

int main() {
    int m, n;
    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];
                ans.insert(a[i]);
            }
            else
                dp[i][j] = max(dp[i][j - 1], dp[i][j]) ;
    fout << ans.size() <<"\n";
    for(auto x : ans){
        fout << x << " ";
    }
    return 0;
}