Cod sursa(job #1452207)

Utilizator petru.cehanCehan Petru petru.cehan Data 20 iunie 2015 12:44:35
Problema Cel mai lung subsir comun Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.01 kb
#include <iostream>
#include <stdio.h>
#include <fstream>
#define maxim(a, b) ((a > b) ? a : b)
using namespace std;
ifstream fin("cmlsc.in");
ofstream fout("cmlsc.out");
int a[101],b[101],i,j,n,m;

void Citire()
{   fin>>n>>m;
    for(i=1;i<=n;++i)
        fin>>a[i];
    for(i=1;i<=m;++i)
        fin>>b[i];
}
int dist[101][101];

void ConstruireMatrice()
{
    for(i=1;i<=n;++i)
      for(j=1;j<=m;++j)
        if(a[i]==b[j])
            dist[i][j]=dist[i-1][j-1]+1;
        else
            dist[i][j]=maxim(dist[i][j-1],dist[i-1][j]);

}

void AfisareSol()
{   int k=0,sol[101];
    i=n,j=m;
    while(i>=0 && j>=0)
       {
           if(a[i]==b[j])
               sol[++k]=a[i],--i,--j;
           else
              if(dist[i-1][j]<dist[i][j-1])
                     --j;
              else
                     --i;
       }
     fout<<k<<"\n";
     for(i=k;i>=1;i--)
        fout<<sol[i];
}

int main()
{
   Citire();
   ConstruireMatrice();
   AfisareSol();
   return 0;
}