Cod sursa(job #2481796)

Utilizator Dumnezeu129081IIsus Hristos Dumnezeu129081 Data 27 octombrie 2019 13:58:20
Problema Cel mai lung subsir comun Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.14 kb
#define MAX_DIM 1024
#define MAX(a, b) (((a) > (b)) ? (a) : (b))

#include <fstream>
using namespace std;

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

int n1, n2, sir1[MAX_DIM + 1], sir2[MAX_DIM + 1], a[MAX_DIM + 1][MAX_DIM + 1];

void Afisare(int x, int y);

int main()
{
    fin >> n1 >> n2;
    for (int i = 1; i <= n1; ++i)
    { fin >> sir1[i]; }
    for (int i = 1; i <= n2; ++i)
    { fin >> sir2[i]; }

    for(int i = 1; i <= n1; ++i)
    {
        for (int j = 1; j <= n2; ++j)
        {
            if (sir1[i] == sir2[j])
            { a[i][j] = a[i - 1][j - 1] + 1; }
            else
            { a[i][j] = MAX(a[i - 1][j], a[i][j - 1]); }

            fout << a[i][j] << ' ';
        }
        fout << '\n';
    }

    fout << "\n\n\n";
    fout << a[n1][n2] << '\n';
    Afisare(n1, n2);

    fin.close();
    fout.close();
    return 0;
}

void Afisare(int x, int y)
{
    if ((x == 0) || (y == 0)) { return; }


    while (a[x][y - 1] > a[x][y]) { --y; }
    while (a[x - 1][y] == a[x][y]) { --x; }

    if (a[x - 1][y - 1] != 0) { Afisare(x - 1, y - 1); }
    fout << sir1[x] << ' ';
}