Cod sursa(job #1205598)

Utilizator Alex1199Alex Bercea Alex1199 Data 7 iulie 2014 14:04:16
Problema Cel mai lung subsir comun Scor 100
Compilator fpc Status done
Runda Arhiva educationala Marime 1.02 kb
var f, g: text;
    m : array[0..1026,0..1026] of integer;
    a,b,c: array[1..1026] of integer;
    i, j, n, l, p : integer;
    buf1:array[1..1 shl 17] of char;

    begin
 Assign(input,'cmlsc.in');
   reset(input);
 settextbuf(input,buf1);
 Assign(output,'cmlsc.out');
 rewrite(output);

  Readln(n,l);
  For i:=1 to n do
    read(a[i]);
  readln;
  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.