Pagini recente » Cod sursa (job #1885426) | Cod sursa (job #970061) | Cod sursa (job #1747092) | Cod sursa (job #1716478) | Cod sursa (job #2791149)
#include<bits/stdc++.h>
using namespace std;
int dp[1026][1026], v[1026], s1[1026], s2[1026];
ifstream f("cmlsc.in");
ofstream g("cmlsc.out");
int main() {
int n1, n2, i, j;
f >> n1 >> n2;
for(i = 1; i <= n1; i++) f >> s1[i];
for(i = 1; i <= n2; i++) f >> s2[i];
for(i = 1; i <= n1; i++) {
for(j = 1; j <= n2; j++) {
if(s1[i] == s2[j]) {
v[++v[0]] = s1[i];
dp[i][j] = dp[i - 1][j - 1] + 1;
}
else {
dp[i][j] = max(dp[i][j-1], dp[i-1][j]);
}
}
}
g << dp[n1][n2] << '\n';
for(i = 1; i <= v[0]; i++) {
g << v[i] << ' ';
}
return 0;
}