Cod sursa(job #2055404)

Utilizator FodosagSera Victor Fodosag Data 3 noiembrie 2017 10:35:03
Problema Cel mai lung subsir comun Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.2 kb
#include <iostream>
#include <fstream>
#include <stack>
#define NMAX 1030

using namespace std;

ifstream f("cmlsc.in");
ofstream g("cmlsc.out");

stack <int> path;

int m, n;
int a[NMAX], b[NMAX];
int dp[NMAX][NMAX];

void Input()
{
    f>>m>>n;
    for (int i = 1; i <= m; ++i)
        f>>a[i];
    for (int i = 1; i <= n; ++i)
        f>>b[i];
}

void d()
{
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= m; ++j)
            dp[i][j] = max(dp[i - 1][j], dp[i][j - 1] + ((a[j] == b[i])? 1 : 0));
}

void getPath()
{
    int i = n;
    int j = m;
    while (i > 0 && j > 0)
    {
        if (dp[i][j - 1] == dp[i][j] - 1 && a[j] == b[i])
        {
            path.push(a[j]);
            j--;
        }
        else if (dp[i - 1][j] == dp[i][j] - 1 && a[j] == b[i])
        {
            path.push(a[j]);
            i--;
        }
        else if (dp[i][j - 1] == dp[i][j])
            j--;
        else if (dp[i - 1][j] == dp[i][j])
            i--;
    }

}

int main()
{
    Input();
    d();
    g << dp[n][m] << "\n";
    getPath();
    while (!path.empty())
    {
        g << path.top() << " ";
        path.pop();
    }
    return 0;
}