Pagini recente » Cod sursa (job #1014175) | Cod sursa (job #2749732) | Cod sursa (job #2829182) | Cod sursa (job #1966799) | Cod sursa (job #3258293)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("cmlsc.in");
ofstream fout("cmlsc.out");
int A[1025],B[1025],mat[1025][1025],v[1025];
int main()
{
int n,m;
fin>>n>>m;
for(int i=1;i<=n;i++)
fin>>A[i];
for(int i=1;i<=m;i++)
{fin>>B[i];}
if(m>n)
{
for(int j=1;j<=m;j++)
{
for(int i=1;i<=n;i++)
{
if(A[i]==B[j])
{
mat[i][j]=mat[i-1][j-1]+1;
int stp=mat[i][j];
v[stp]=A[i];
}
else
mat[i][j]=max(mat[i][j-1],mat[i-1][j]);
}
}
fout<<mat[n][m]<<'\n';
for(int i=1;i<=mat[n][m];i++)
fout<<v[i]<<' ';
}
else
{
for(int j=1;j<=n;j++)
{
for(int i=1;i<=m;i++)
{
if(A[j]==B[i])
{
mat[i][j]=mat[i-1][j]+1;
int stp=mat[i][j];
v[stp]=A[j];
}
else
mat[i][j]=max(mat[i][j-1],mat[i-1][j]);
}
}
fout<<mat[m][n]<<'\n';
for(int i=1;i<=mat[m][n];i++)
fout<<v[i]<<' ';
}
}