Cod sursa(job #157219)

Utilizator ghitza_2000Stefan Gheorghe ghitza_2000 Data 12 martie 2008 21:52:28
Problema Cel mai lung subsir comun Scor 90
Compilator fpc Status done
Runda Arhiva educationala Marime 0.9 kb
var i,j,m,n,p:longint;
a,b,d:array[1..1025] of longint;
c:array[1..1025,1..1025] of longint;
f,g:text;
procedure citire(var m,n:longint);
var i,j:longint;
begin
readln(f,m,n);
for i:=1 to m do
read(f,a[i]);
for j:=1 to n do
read(f,b[j]);
readln(f);
end;
function max(n,m:longint):longint;
begin
if n>m then max:=n
else max:=m;
end;
begin
assign(f,'cmlsc.in'); reset(f);
assign(g,'cmlsc.out'); rewrite(g);
citire(m,n);
for i:=1 to m do
  for j:=1 to n do
     if a[i]=b[j]
      then c[i,j]:=1+c[i-1,j-1]
      else c[i,j]:=max(c[i-1,j],c[i,j-1]);
i:=m; j:=n; p:=1;
 while (i*j>0) do
 if a[i]=b[j]then begin
                     d[p]:=a[i];
                     dec(i);
                     dec(j);
                     inc(p);
                  end
 else if c[i-1,j]<c[i,j-1] then dec(j)
 else dec(i);
writeln(g,p-1);
for i:=p-1 downto 1 do
write(g,d[i],' ');
close(f); close(g);
end.