I and OI
Past...
var   f:array[0..101,0..101] of longint;
      a,b:array[0..101] of longint;
      dnum,dx,n1,n2,i,j,p,q:longint;


      function max(a,b:longint):longint;
      begin if a>b then exit(a); exit(b); end;

begin
      readln(dnum);
      for dx:=1 to dnum do
      begin
            fillchar(f,sizeof(f),0);
            readln(n1,n2);
            for i:=1 to n1 do read(a[i]); readln;
            for i:=1 to n2 do read(b[i]); readln;
            for i:=1 to n1 do
               for j:=1 to n2 do
               begin
                     f[i,j]:=max(f[i-1,j],f[i,j-1]);
                     if a[i]=b[j] then continue;
                     for p:=i-1 downto 1 do
                        if a[p]=b[j] then break;
                     if a[p]=b[j] then
                     begin
                           for q:=j-1 downto 1 do
                              if b[q]=a[i] then break;
                           if b[q]=a[i] then
                             f[i,j]:=max(f[i,j],f[p-1,q-1]+2);
                     end;
               end;
            writeln(f[n1,n2]);
      end;
end.


 

题意:给出两行数,求上下匹配的最多组数是多少.
匹配规则
1.匹配对的数字必须相同
2.每个匹配必须有且只能有一个匹配与之相交叉,且相交叉的两组匹配数字必须不同
3.一个数最多只能匹配一次

分析:DP.f[i,j]表示上面到i,下面到j的匹配对数.

f[i,j]=max(f[i-1,j],f[i,j-1],max{f[p-1,q-1]+2} (a[p]=b[j] & b[q]=a[i])).

code:

posted on 2011-08-10 16:04  exponent  阅读(297)  评论(0编辑  收藏  举报