Cod sursa(job #2025443)

Utilizator shantih1Alex S Hill shantih1 Data 22 septembrie 2017 18:28:23
Problema Cel mai lung subsir comun Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.92 kb
#include <iostream>
#include <fstream>

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

int n, m, i, j, nr, r1, a[1100], b[1100], rez[1100], best[1100][1100];
short trace[1100][1100];

int main () {
    
    fin >> n >> m;
    for (i = 1; i <= n; i++)
        fin >> a[i];
    for (j = 1; j <= m; j++)
        fin >> b[j];
    
    for (i = 1; i <= n; i++)
        for (j = 1; j <= m; j++)
        {
            if (a[i] == b[j])
                best[i][j] = best[i-1][j-1] + 1;
                
            else
                best[i][j] = max(best[i-1][j], best[i][j-1]);
        }
    
    nr = 0;
    i = n;  j = m;
    while (i != 0 && j != 0)
    {
        if (a[i] == b[j])   {   nr++;   rez[nr] = a[i];     i--;    j--;    }
        
        else if (best[i-1][j] >= best[i][j-1])   i--;
        else    j--;
    }
    
    fout << nr << "\n";
    for (i = nr; i >= 1; i--)
        fout << rez[i] << " ";
}