Cod sursa(job #1032933)

Utilizator ValeraShulghin Valera Valera Data 16 noiembrie 2013 11:10:11
Problema Cel mai lung subsir comun Scor 20
Compilator fpc Status done
Runda Arhiva educationala Marime 0.68 kb
var a,b:array[1..1024] of integer;
    c:array[0..1024,0..1024] of integer;
    m,n,i,j,k,max:longint;
    f,g:text;
begin
assign(f,'cmlsc.in');reset(f);
assign(g,'cmlsc.out');rewrite(g);
readln(f,m,n);
for i:=1 to m do
  read(f,a[i]);
for j:=1 to n do
  read(f,b[j]);
for i:=1 to m do
  for j:=1 to n do
    begin
      if a[i]=b[j] then c[i,j]:=c[i-1,j-1]+1
      else if c[i-1,j]>c[i,j-1] then c[i,j]:=c[i-1,j] else c[i,j]:=c[i,j-1];
      if c[i,j]>max then max:=c[i,j];
    end;
writeln(g,max);
k:=1;
while k<=max do
  for i:=1 to m do
    for j:=1 to n do
      if (a[i]=b[j]) and (c[i,j]=k) then begin write(g,a[i],' ');k:=k+1;end;


close(f);close(g);
end.