Pagini recente » Monitorul de evaluare | Cod sursa (job #799442) | Cod sursa (job #1227901) | Cod sursa (job #1046908) | Cod sursa (job #2133680)
#include <fstream>
#define DMAX 1026
using namespace std;
ifstream fin("cmlsc.in");
ofstream fout("cmlsc.out");
int n,m;
int a[DMAX];
int b[DMAX];
int lgmax[DMAX][DMAX];
int unde[DMAX][DMAX];
int main()
{
int i,j;
fin>>n;
for(i=1;i<=n;i++)
fin>>a[i];
fin>>m;
for(i=1;i<=m;i++)
fin>>b[i];
for(i=n;i>=1;i--)
for(j=m;j>=1;j--)
{
if(a[i]==b[j])
{
lgmax[i][j]=1+lgmax[i+1][j+1];
unde[i][j]=1;
}
else
{
if(lgmax[i+1][j]>lgmax[i][j+1])
{
lgmax[i][j]=lgmax[i+1][j];
unde[i][j]=2;
}
else
{
lgmax[i][j]=lgmax[i][j+1];
unde[i][j]=3;
}
}
}
//afisare
fout<<lgmax[1][1]<<"\n";
i=1;
j=1;
while(i<=n&&j<=m)
{
if(unde[i][j]==1)
{
fout<<a[i]<<" ";
i++;
j++;
}
else if(unde[i][j]==2)
{
i++;
fout<<a[i]<<" ";
}
else
{
j++;
fout<<b[j]<<" ";
}
}
return 0;
}