Cod sursa(job #1234544)

Utilizator duplava.anaDuplava Ana duplava.ana Data 27 septembrie 2014 15:31:33
Problema Cel mai lung subsir comun Scor 100
Compilator fpc Status done
Runda Arhiva educationala Marime 0.87 kb
program cmlsc;
var m:array[0..1026,0..1026] of integer;
    a, b, c: array[1..1026] of integer;
    i,j,n,l,p: integer;
begin
   assign(input, 'cmlsc.in');
   reset(input);
   assign(output, 'cmlsc.out');
   rewrite(output);

   readln(n,l);
   for i:=1 to n do read(a[i]);
   for i:=1 to l do read(b[i]);

   for i:=1 to n do
      for j:=1 to l do
          if a[i]=b[j] then m[i,j]:=m[i-1,j-1]+1
                       else
                            if m[i-1,j]>m[i,j-1] then m[i,j]:=m[i-1,j]
                                                 else m[i,j]:=m[i,j-1];

   writeln(m[n,l]);

   while m[i,j]<>0 do
        begin
         while m[i,j-1]=m[i,j] do dec(j);
         while m[i-1,j]=m[i,j] do dec(i); inc(p);
         c[p]:=a[i];
         dec(i);
        end;
   for i:=m[n,l] downto 1 do write (c[i],' ');
   close (input);
   close (output);
end.