Cod sursa(job #1226725)

Utilizator Ionut228Ionut Calofir Ionut228 Data 6 septembrie 2014 23:39:43
Problema Cel mai lung subsir comun Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.07 kb
#include <fstream>
#include <algorithm>

using namespace std;

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

int N, M;
int sir[1025], lg;
int A[1025], B[1025], DP[1025][1025];

void cmlsc()
{
    for (int i = 1; i <= N; ++i)
        for (int j = 1; j <= M; ++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]);
        }
    fout << DP[N][M] << '\n';

    int x = N, y = M;
    while (true)
    {
        if (A[x] == B[y])
        {
            sir[++lg] = A[x];
            --x;
            --y;
            if (!DP[x][y]) break;
        }
        else if (DP[x - 1][y] != DP[x][y - 1])
            --y;
        else
            --x;
    }

    for (int i = lg; i >= 1; --i)
        fout << sir[i] << ' ';
    fout << '\n';
}

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

    cmlsc();

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