Cod sursa(job #2427898)

Utilizator cristicretancristi cretan cristicretan Data 2 iunie 2019 18:06:34
Problema Cel mai lung subsir comun Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.38 kb
/*
* @Author: Cristi Cretan
* @Date:   02-06-2019 17:40:00
* @Last Modified by:   Cristi Cretan
* @Last Modified time: 02-06-2019 18:06:28
*/
#include <bits/stdc++.h>
// #define f cin
// #define g cout
#define dbg(x) cerr<<#x<<" = "<<x<<endl;
#define dbg_v(v,n) {cerr<<#v<<" = [";for(int III=1;III<=n;III++)cerr<<v[III]<<(III!=n?",":"]\n");}
#define ll long long
#define ld long double
#define pii pair<int,int>
#define MOD 1000000007
#define zeros(x) x&(x-1)^x
#define NMax 1027
using namespace std;

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

int A, B;
int a[NMax], b[NMax], dp[NMax][NMax];
int ans[NMax], k;

int main()
{
    f >> A >> B;

    for (int i = 1; i <= A; ++i)
        f >> a[i];
    for (int i = 1; i <= B; ++i)
        f >> b[i];

    for (int i = 1; i <= A; ++i)
        for (int j = 1; j <= B; ++j)
            if (a[i] == b[j])
                dp[i][j] = dp[i - 1][j - 1] + 1;
            else
                dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);

    g << dp[A][B] << '\n';

    int i_curent = A;
    int j_curent = B;


    while(i_curent) {
        if (a[i_curent] == b[j_curent]) {
            ans[++k] = a[i_curent];
            i_curent--;
            j_curent--;
        }
        else if (dp[i_curent - 1][j_curent] < dp[i_curent][j_curent - 1]) --j_curent;
        else --i_curent;
    }

    for (int i = k; i >= 1; --i)
        g << ans[i] << " ";
    return 0;
}