Cod sursa(job #3227534)

Utilizator tudorororTudor-Mihail Danila tudororor Data 1 mai 2024 19:00:36
Problema Cel mai lung subsir comun Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.73 kb
#include <bits/stdc++.h>

using namespace std;

ifstream f("cmlsc.in");
ofstream g("cmlsc.out");

stack <int> rez;
int dp[1025][1025];
int main()
{
    int m, n, a[1025], b[1025];
    f >> m >> n;
    for(int i = 1; i <= m; i++)
        f >> a[i];
    for(int i = 1; i <= n; i++)
        f >> b[i];
    for(int i = n; i >= 1; i--)
        for(int j = m; j >= 1; j--)
            if(a[j] == b[i])
            {
                dp[i][j] = 1 + dp[i + 1][j + 1];
                rez.push(a[j]);
            }
            else
                dp[i][j] = max(dp[i + 1][j], dp[i][j + 1]);
    g << dp[1][1] << '\n';
    while(!rez.empty())
    {
        g << rez.top() << ' ';
        rez.pop();
    }
    return 0;
}