Cod sursa(job #2367032)

Utilizator dan.ghitaDan Ghita dan.ghita Data 5 martie 2019 00:35:02
Problema Cel mai lung subsir comun Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.74 kb
#include <bits/stdc++.h>

using namespace std;

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

int m, n, x, cmlsc[1027][1027], t[1027];
vector<int> a, b;

int main()
{
    f >> m >> n;
    
    // index form 1
    a.push_back(0);
    b.push_back(0);

    while (m--) f >> x, a.push_back(x);
    while (n--) f >> x, b.push_back(x);

    for (int i = 1; i < a.size(); ++i)
        for (int j = 1; j < b.size(); ++j)
            cmlsc[i][j] = max(cmlsc[i - 1][j], cmlsc[i][j - 1]) + (a[i] == b[j]);

    g << cmlsc[a.size() - 1][b.size() - 1] << '\n';
    
    int cnt = 1;
    for (int j = 0; j < b.size(); ++j)
        if (cmlsc[a.size() - 1][j] == cnt)
            g << b[j] << ' ',
            ++cnt;

    return 0;
}