Cod sursa(job #2856159)

Utilizator newagear2Dragan Iulian newagear2 Data 23 februarie 2022 14:55:30
Problema Cel mai lung subsir comun Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.15 kb
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
vector<short> v1,v2;
vector<short> x, vMax;
int n,m, maxLength=0;
ifstream cin("cmlsc.in");
ofstream cout("cmlsc.out");
bool valid(){
    int contor = 0;
    for(auto it : x){
        auto iter = find(v2.begin()+contor, v2.end(), it);
        if(iter==v2.end()){
            return 0;
        }
        contor = distance(v2.begin(), iter);
    }
    return 1;
}
void changeMax(){
    if(x.size()>maxLength){
        vMax = x;
        maxLength = x.size(); 
    }
}
void back(int k){
    for(int i=k; i<n; i++){
        x.push_back(v1[i]);
        if(valid()){
            changeMax();
            back(i+1);
            x.pop_back();
        }
        else{
            x.pop_back();
        }
    }
}
int main()
{
    cin>>n>>m;
    for(int i=0;i<n;i++){
        int local;
        cin>>local;
        v1.push_back(local);
    }
    for(int i=0;i<m;i++){
        int local;
        cin>>local;
        v2.push_back(local);
    }
    back(0);
    cout<<maxLength<<'\n';
    for(auto it : vMax){
        cout<<it<<" ";
    }
    return 0;
}