状压动规_(POJ2817)

题意:给出N(N<=10)个字符串(length<=10),定义两个串a,b之间的公共序列长度为

将a,b对齐后,相同位置上相同字母的个数,a,b的最长公共序列长度自然是相同字母数的

最大值,如a='abcd',b='bed',a,b的最长公共序列长度为2.

如要求将N个字符串排列,使得相邻的N-1对字符串的最长公共序列长度和最大.

(出题人不负责,N怎么也应该有20,这题暴搜可以过.)

讲讲状压的做法.

f[i,j]中j(二进制)表示哪些字符串已选,i表示最右边的是哪一个字符串.

转移时,枚举j中的一个不为0的位,去掉,成为j',i'表示j'中所有不为0的位,

则f[i,j]=max{f[i',j']+LCS(s[i],s[i'])}  ((j'>>(i'-1))&1=1)

(LCS是上面定义的那种).

ans=max{f[i,1<<n-1]}  (1<=i<=n)

code:

type  stringx=string[11];
var   f:array[0..11,0..1100] of longint;
      g:array[0..11,0..11] of longint;
      s:array[0..11] of stringx;
      n,i,j,opt,now,ans:longint;


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

      function work(a,b:stringx):longint;
      var   p,q,la,lb,l,c,maxl:longint;
      begin
            maxl:=0;
            la:=length(a);
            lb:=length(b);
            for p:=1 to la do
               for q:=1 to lb do
               begin
                     l:=0;
                     c:=0;
                     while (p+l<=la)and(q+l<=lb) do
                     begin
                           if a[p+l]=b[q+l] then inc(c);
                           inc(l);
                     end;
                     if c>maxl then maxl:=c;
               end;
            exit(maxl);
      end;

      function check(num:longint):boolean;
      var   c:longint;
      begin
            c:=0;
            while num>0 do
            begin
                  inc(c,num and 1);
                  num:=num>>1;
            end;
            exit(c>1);
      end;

begin
      while not seekeof do
      begin
            readln(n);
            if n<=0 then break;
            for i:=1 to n do readln(s[i]);
            for i:=1 to n-1 do
               for j:=i+1 to n do
               begin
                     g[i,j]:=work(s[i],s[j]);
                     g[j,i]:=g[i,j];
               end;


            fillchar(f,sizeof(f),171);
            for i:=1 to n do f[i,1<<(i-1)]:=0;

            for opt:=1 to 1<<n-1 do
               if check(opt) then
               for i:=1 to n do
                  if (opt>>(i-1)) and 1=1 then
                  begin
                        now:=opt xor (1<<(i-1));
                        for j:=1 to n do
                           if (now>>(j-1)) and 1=1 then
                           f[i,opt]:=max(f[i,opt],f[j,now]+g[j,i]);
                  end;
            ans:=0;
            for i:=1 to n do
               ans:=max(ans,f[i,1<<n-1]);

            writeln(ans);
      end;
end.


 

note:求最大权哈密顿回路可以用状态压缩的方法,类似此题.

另外一些摆放类的问题求最值也可以用状压(以后会写).

转载于:https://www.cnblogs.com/exponent/archive/2011/08/07/2130146.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值