Cod sursa(job #2849905)

Utilizator MihaiCostacheCostache Mihai MihaiCostache Data 15 februarie 2022 22:25:25
Problema Cel mai lung subsir comun Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.12 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("cmlsc.in");
ofstream fout("cmlsc.out");
int n, m, a[1030], b[1030], dp[1030][1030];
stack<int> sol;
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];
    }
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; 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]);
            }
        }
    }
    int lung=dp[n][m], i=n, j=m;
    fout<<lung<<"\n";
    while(lung!=0)
    {
        if(a[i]==b[j])
        {
            sol.push(a[i]);
            lung--;
            i--;
            j--;
        }
        else
        {
            if(dp[i-1][j]>dp[i][j-1])
            {
                i--;
            }
            else
            {
                j--;
            }
        }
    }
    while(!sol.empty())
    {
        fout<<sol.top()<<" ";
        sol.pop();
    }
    return 0;
}