Cod sursa(job #1414565)

Utilizator o_micBianca Costin o_mic Data 2 aprilie 2015 19:19:50
Problema Cel mai lung subsir comun Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.36 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
#define LL long long 
#define MAX 1030
 
using namespace std;

vector <int> a, b;
int dmax[MAX][MAX];
stack <int> s;

int main()
{
    int m, n, x;
    ifstream fin("cmlsc.in");
    //ifstream fin("input.txt");
    ofstream fout("cmlsc.out");
    fin >> m >> n;
    a.push_back(0);
    for(int i = 0; i < m; ++i){
        fin >> x;
        a.push_back(x);
    }
    b.push_back(0);
    for(int i = 0; i < n; ++i){
        fin >> x;
        b.push_back(x);
    }

    for(int i = 1; i <= m; ++i){
        for(int j = 1; j <= n; ++j){
            if(a[i] == b[j])
                dmax[i][j] = dmax[i-1][j-1] + 1;
            else
                dmax[i][j] = max(dmax[i-1][j], dmax[i][j-1]);
        }
    }
    fout << dmax[m][n] << '\n';

    while(m > 1 && n > 1){
        if(a[m] == b[n]){
            s.push(a[m]);
            m--;
            n--;
        }
        else{
            if(dmax[m-1][n] >= dmax[m][n-1])
                --m;
            else
                --n;
        }
    }
    if(m == 1){
        while(dmax[m][n] != 0)
            --n;
        ++n;
        s.push(b[n]);
    }
    if(n == 1){
        while(dmax[m][n] != 0)
            --m;
        ++m;
        s.push(a[m]);
    }
    while(!s.empty()){
        x = s.top();
        s.pop();
        fout << x << " ";
    }
    return 0;
}