Cod sursa(job #2729386)

Utilizator emanuel2186Lugojan Emanuel emanuel2186 Data 24 martie 2021 17:48:24
Problema Cel mai lung subsir comun Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.21 kb
#include <bits/stdc++.h>
#define Nmax 1030
using namespace std;
ifstream fin("cmlsc.in");
ofstream fout("cmlsc.out");
short int N, M;
short int a[Nmax];
short int b[Nmax];
vector<short int> sir;
int L[Nmax][Nmax];
void rezolvare()
{
    for(int i=1; i<=N; i++)
    {
        for(int j=1; j<=M; j++)
        {
            if(a[i] == b[j])
            {
                L[i][j] = L[i-1][j-1] + 1;
            }
            else
            {
                L[i][j] = max(L[i-1][j], L[i][j-1]);
            }
        }
    }
    fout<<L[N][M]<<"\n";
    int contor = L[N][M];
    int i = N, j = M;
    while(i > 0 && j > 0 && contor > 0)
    {
        if(a[i] == b[j])
        {
            sir.push_back(b[j]);
            i--;
            j--;
        }
        else
        {
            if(L[i-1][j] > L[i][j - 1])
                i--;
            else
                j--;
        }
    }
    for(int i = sir.size() - 1; i>=0; i--)
        fout<<sir[i]<<" ";
}
void citire()
{
    fin>>N>>M;
    for(int i=1; i<=N; i++)
    {
        fin>>a[i];
    }
    for(int j=1; j<=M; j++)
    {
        fin>>b[j];
    }
    rezolvare();
}
int main()
{
    citire();
    return 0;
}