Cod sursa(job #1380254)

Utilizator hopingsteamMatraguna Mihai-Alexandru hopingsteam Data 7 martie 2015 04:32:46
Problema Cel mai lung subsir comun Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.07 kb
#include    <iostream>
#include    <fstream>

using namespace std;

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

const int NLIM = 1030;

int N, M;
int A[NLIM], B[NLIM];
int DP[NLIM][NLIM];
int V[NLIM], answers;

void Read()
{
    fin >> M >> N;
    for(int i = 1; i <= M; i++)
        fin >> A[i];
    for(int j = 1; j <= N; j++)
        fin >> B[j];

    for(int i = 1; i <= M; i++)
    {
        for(int j = 1; j <= N; j++)
        {
            if(A[i] == B[j])
                DP[i][j] = 1 + DP[i-1][j-1];
            else
                DP[i][j] = max(DP[i-1][j], DP[i][j-1]);
        }
    }

    for(int i = M, j = N; ;)
    {
        if(A[i] == B[j])
        {
            V[++answers] = A[i];
            i -= 1; j -= 1;
        }

        if(DP[i-1][j] < DP[i][j-1])
            j -= 1;
        else
            i -= 1;

        if(i == 0 || j == 0)
            break;
    }

    fout << answers << "\n";
    for(int i = answers; i > 0; i--)
        fout << V[i] << " ";
}

int main()
{
    Read();
    return 0;
}