Pagini recente » Cod sursa (job #2422971) | Cod sursa (job #3162131) | Cod sursa (job #681085) | Cod sursa (job #391395) | Cod sursa (job #2685672)
#include <fstream>
using namespace std;
ifstream fin ("cmlsc.in");
ofstream fout ("cmlsc.out");
int n, m, a[1025], b[1025], r[1025], sclm[1025][1025], l;
int main()
{
fin >> n >> m;
for (int i = 1; i <= n; i++)
fin >> a[i];
for (int j = 1; j <= m; j++)
fin >> b[j];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (a[i] == b[j])
sclm[i][j] = sclm[i - 1][j - 1] + 1;
else
sclm[i][j] = max (sclm[i - 1][j], sclm[i][j - 1]);
int i = n, j = m;
while (i >= 1 and j >= 1) {
if (a[i] == b[j]) {
r[++l] = a[i];
i--; j--;
}
else
if (sclm[i - 1][j] > sclm[i][j - 1])
i--;
else
j--;
}
fout << l << '\n';
for (; l >= 1; l--)
fout << r[l] << ' ';
return 0;
}