Cod sursa(job #3273964)

Utilizator _adeee18Adelina Maria _adeee18 Data 4 februarie 2025 17:29:46
Problema Cel mai lung subsir comun Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.93 kb
#include <fstream>
#include <vector>
using namespace std;

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

vector <int> a, b, s;
int lmax;
struct date
{
    int fr, poz;
};
vector <date> v;

int main()
{
    int n, m;
    fin >> n >> m;
    if(n < m)
    {
        v.resize(257);
        for(int i = 0 ;i < n; i++)
        {
            int x;
            fin >> x;
            a.push_back(x);
        }
        for(int i = 0; i < m; i++)
        {
            int x;
            fin >> x;
            b.push_back(x);
            v[x].fr++;
            if(v[x].fr == 1)
                v[x].poz = i;

        }
    }
    else
    {
        v.resize(257);
        for(int i = 0; i < n; i++)
        {
            int x;
            fin >> x;
            b.push_back(x);
            v[x].fr++;
            if(v[x].fr == 1)
                v[x].poz = i;

        }
        for(int i = 0 ;i < m; i++)
        {
            int x;
            fin >> x;
            a.push_back(x);
        }
    }
    for(int i = 0; i < a.size(); i++)
    {
        if(v[a[i]].fr!= 0)
        {
            if(v[a[i]].fr == 1)
              s.push_back(v[a[i]].poz);
            else
            {
                s.push_back(v[a[i]].poz);
                v[a[i]].poz++;
                while(a[i] != a[v[a[i]].poz] && v[a[i]].poz < a.size())
                    v[a[i]].poz++;
            }
        }
    }
    a.clear();
    a.resize(s.size());
    for(int i = s.size() - 1; i > 0; i--)
    {
        for(int j = i - 1; j >= 0; j--)
        {
            if(s[i] > s[j])
            {
                a[j] = max(a[j], a[i] + 1);
                lmax = max(lmax, a[j]);
            }
        }
    }
    fout << lmax + 1 << "\n";
    for(int i = 0; i < a.size(); i++)
    {
        if(a[i] == lmax)
        {
            fout << b[s[i]] << ' ';
            lmax--;
        }
    }
    return 0;
}