Cod sursa(job #3166946)

Utilizator AlexInfoIordachioaiei Alex AlexInfo Data 9 noiembrie 2023 20:00:47
Problema Cel mai lung subsir comun Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.96 kb
#include <bits/stdc++.h>

using namespace std;

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

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
#define fi first
#define se second

#define NMAX 1030
#define MOD 1999999973
#define INF 0x3f3f3f3f

int n, m, a[NMAX], b[NMAX], LCS[NMAX][NMAX];
vector<int> ans;

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

void lcs();

void solve()
{
    lcs();
    out << LCS[n][m] << '\n';
    for (auto it : ans)
        out << it << ' ';
}

void lcs()
{
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            if (a[i] == b[j])
                LCS[i][j] = LCS[i - 1][j - 1] + 1, ans.push_back(a[i]);
            else
                LCS[i][j] = max(LCS[i - 1][j], LCS[i][j - 1]);
}

int main()
{
    read();
    solve();
    return 0;
}