Cod sursa(job #2864056)

Utilizator BlueLuca888Girbovan Robert Luca BlueLuca888 Data 7 martie 2022 15:42:04
Problema Cel mai lung subsir comun Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.02 kb
#include <bits/stdc++.h>

using namespace std;

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

const int DIM = 1050;

int dp[DIM][DIM], a[DIM], b[DIM];
int n, m, sol;

stack <int> q;

int main (){
    ios_base::sync_with_stdio(false);
    fin.tie(nullptr);
    fout.tie(nullptr);

    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])
                dp[i][j] = 1 + dp[i-1][j-1];
            else
                dp[i][j] = max(dp[i-1][j], dp[i][j-1]);

    sol = dp[n][m];
    fout<<sol<<"\n";
    while(sol != 0){
        if(dp[n][m] == dp[n-1][m-1] + 1){
            q.push(a[n]);
            sol--;
            n--;
            m--;
        }else{
            if(dp[n-1][m] < dp[n][m-1])
                m--;
            else
                n--;
        }
    }
    while(!q.empty()){
        fout<<q.top()<<" ";
        q.pop();
    }
    return 0;
}