Cod sursa(job #1332435)

Utilizator ggokGeri Gokaj ggok Data 1 februarie 2015 23:43:28
Problema Cel mai lung subsir comun Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 0.76 kb
#include <stdio.h>
#include <fstream>
#include <vector>
using namespace std;
int d[1000][1000];
int a[1000];
int b[1000];
int sol[1000];
int main()
{
    freopen("cmlsc.in","r",stdin);
    freopen("cmlsc.out","w",stdout);
    int N,M;
    int counter;
    scanf("%d",&N);
    scanf("%d",&M);
for(int x=1;x<=N;x++)
scanf("%d",&a[x]);
for(int x=1;x<=M;x++)
    scanf("%d",&b[x]);

for(int x=1;x<=N;x++)
    for(int y=1;y<=M;y++)
    if(a[x]==b[y])
    d[x][y]=d[x-1][y-1]+1;
else
    d[x][y]=max(d[x-1][y],d[x][y-1]);
counter=1;
int l=0;
for(int x=1;x<=N;x++)
    for(int y=1;y<=M;y++)
    if(d[x][y]==counter){
    sol[counter++]=a[x];
    l++;

}
printf("%d \n",l);
for(int x=1;x<counter;x++)
    printf("%d ",sol[x]);
    return 0;
}