Cod sursa(job #3338916)

Utilizator st3fann_24Nica Stefan st3fann_24 Data 5 februarie 2026 14:13:08
Problema Cel mai lung subsir comun Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.86 kb
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define MOD 1000000007

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

short n, m, dp[1025][1025];
int main(){
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    std::cout.tie(NULL);
    // f.tie(NULL);
    // g.tie(NULL);
    
    f >> n >> m;
    std::vector<int> a(n + 5), b(m + 5);
    for(int i = 1; i <= n; ++i)
        f >> a[i];
    for(int j = 1; j <= m; ++j)
        f >> b[j];
    for(int i = 1; i <= m; ++i)
        for(int j = 1; j <= n; ++j)
            if(b[i] == a[j])
                dp[i][j] = dp[i - 1][j - 1] + (b[i] == a[j]);
            else 
                dp[i][j] = std::max({dp[i][j - 1], dp[i - 1][j]});
    g << dp[n][m] << '\n';
    for(int i = 1; i <= dp[n][m]; ++i)
        g << 1 << ' ';
    return 0;
}