Cod sursa(job #582511)

Utilizator danalex97Dan H Alexandru danalex97 Data 15 aprilie 2011 14:24:49
Problema Cel mai lung subsir comun Scor 100
Compilator fpc Status done
Runda Arhiva educationala Marime 0.77 kb
var n,m,i,j,k:longint;
a,b,c:array[1..1024] of byte;
f,g:text;
l:array[0..1024,0..1024] of integer;

begin
assign(f,'cmlsc.in');reset(f);
assign(g,'cmlsc.out');rewrite(g);
read(f,n,m);
for i:=1 to n do
read(f,a[i]);
readln(f);
for j:=1 to m do
read(f,b[j]);
for i:=1 to n do
  for j:=1 to m do
    if  a[i]=b[j] then l[i,j]:=l[i-1,j-1]+1
      else
    if l[i-1,j]>l[i,j-1] then l[i,j]:=l[i-1,j]
      else l[i,j]:=l[i,j-1];
writeln(g,l[n,m]);
k:=0;
i:=n;
j:=m;
while (i<>0) and (j<>0) do
   if a[i]=b[j] then
     begin
       inc(k);
       c[k]:=a[i];
       dec(i);
       dec(j);
     end
   else
     if l[i,j]=l[i-1,j] then dec(i)
   else
     if l[i,j]=l[i,j-1] then dec(j);
for i:=k downto 1 do
write(g,c[i],' ');
close(f);
close(g);
end.