【BZOJ3864】Hero meet devil

Description

There is an old country and the king fell in love with a devil. The devil always asks the king to do some crazy things. Although the king used to be wise and beloved by his people. Now he is just like a boy in love and can’t refuse any request from the devil. Also, this devil is looking like a very cute Loli.
After the ring has been destroyed, the devil doesn't feel angry, and she is attracted by z*p's wisdom and handsomeness. So she wants to find z*p out.
But what she only knows is one part of z*p's DNA sequence S leaving on the broken ring.
Let us denote one man's DNA sequence as a string consist of letters from ACGT. The similarity of two string S and T is the maximum common subsequence of them, denote by LCS(S,T).
After some days, the devil finds that. The kingdom's people's DNA sequence is pairwise different, and each is of length m. And there are 4^m people in the kingdom.
Then the devil wants to know, for each 0 <= i <= |S|, how many people in this kingdom having DNA sequence T such that LCS(S,T) = i.
You only to tell her the result modulo 10^9+7.

Input

The first line contains an integer T, denoting the number of the test cases.
For each test case, the first line contains a string S. the second line contains an integer m.
T<=5
|S|<=15. m<= 1000.

Output

For each case, output the results for i=0,1,...,|S|, each on a single line.

Sample Input

1
GTC
10

Sample Output

1
22783
528340
497452

题意:给你一个串S,问所有长度为m的字符串中,与S串的最长公共子序列长度为1...|S|的串的个数。

题解:话说这种DP套DP的题最近有点流行~

还记得怎么求最长公共子序列吗?记得那个求最长公共子序列时的矩阵吗?不记得我就再说一遍。

令f[i][j]表示T串中到了第i个数,S串中到了第j个数,的LCS的长度。那么经典的DP方程:

$f[i][j]=max(f[i-1][j],f[i][j-1],(T[i]==S[j])?(f[i-1][j-1]+1):0)$

好了,但是我们求的是方案数,如果直接这样DP的话,需要记录的状态非常多(当前T可能的字符,之前T可能的字符。。。)。但是我们发现S的长度非常小,可以考虑把它单独拿出来处理一下。

因为每一行只能从上一行转移过来,我们不妨状压所有可能的行,暴力计算出在T中添加一个字符后会转移到哪个行。但是行中每一位的数不是0/1,差分一下就好了。处理出所有的转移后,再跑一个DP统计答案就行了。

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <iostream>
  4. #include <cstring>
  5. using namespace std;
  6. const int mod=1000000007;
  7. int n,m;
  8. int to[1<<16][4],s[20],cnt[1<<16],f[2][1<<16],ans[20];
  9. char str[20];
  10. void init()
  11. {
  12. memset(to,0,sizeof(to));
  13. memset(cnt,0,sizeof(cnt));
  14. memset(f,0,sizeof(f));
  15. memset(ans,0,sizeof(ans));
  16. int i,j,k,s1,s2,t1,t2,tar;
  17. for(i=0;i<(1<<n);i++)
  18. {
  19. if(i) cnt[i]=cnt[i-(i&-i)]+1;
  20. for(j=0;j<4;j++)
  21. {
  22. for(tar=s1=s2=t1=t2=0,k=0;k<n;k++)
  23. {
  24. t1=s1,t2=s2,s2+=((i>>k)&1),s1=max(t1,s2);
  25. if(s[k]==j) s1=max(s1,t2+1);
  26. tar|=((s1-t1)<<k);
  27. }
  28. to[i][j]=tar;
  29. }
  30. }
  31. }
  32. void work()
  33. {
  34. scanf("%s%d",str,&m),n=strlen(str);
  35. int i,j;
  36. for(i=0;i<n;i++)
  37. {
  38. if(str[i]=='A') s[i]=0;
  39. if(str[i]=='G') s[i]=1;
  40. if(str[i]=='C') s[i]=2;
  41. if(str[i]=='T') s[i]=3;
  42. }
  43. init();
  44. f[0][0]=1;
  45. for(j=0;j<=m;j++)
  46. {
  47. for(i=0;i<(1<<n);i++) f[(j&1)^1][i]=0;
  48. for(i=0;i<(1<<n);i++) f[(j&1^1)][to[i][0]]=(f[j&1^1][to[i][0]]+f[j&1][i])%mod,f[(j&1^1)][to[i][1]]=(f[j&1^1][to[i][1]]+f[j&1][i])%mod,f[(j&1^1)][to[i][2]]=(f[j&1^1][to[i][2]]+f[j&1][i])%mod,f[(j&1^1)][to[i][3]]=(f[j&1^1][to[i][3]]+f[j&1][i])%mod;
  49. }
  50. for(i=0;i<(1<<n);i++) ans[cnt[i]]=(ans[cnt[i]]+f[m&1][i])%mod;
  51. for(i=0;i<=n;i++) printf("%d\n",ans[i]);
  52. }
  53. int main()
  54. {
  55. int T;
  56. scanf("%d",&T);
  57. while(T--) work();
  58. return 0;
  59. }

【BZOJ3864】Hero meet devil DP套DP的更多相关文章

  1. BZOJ3864: Hero meet devil【dp of dp】

    Description There is an old country and the king fell in love with a devil. The devil always asks th ...

  2. bzoj千题计划241:bzoj3864: Hero meet devil

    http://www.lydsy.com/JudgeOnline/problem.php?id=3864 题意: 给你一个DNA序列,求有多少个长度为m的DNA序列和给定序列的LCS为0,1,2... ...

  3. BZOJ3864: Hero meet devil(dp套dp)

    Time Limit: 8 Sec  Memory Limit: 128 MBSubmit: 397  Solved: 206[Submit][Status][Discuss] Description ...

  4. HDU 4899 Hero meet devil (状压DP, DP预处理)

    题意:给你一个基因序列s(只有A,T,C,G四个字符,假设长度为n),问长度为m的基因序列s1中与给定的基因序列LCS是0,1......n的有多少个? 思路:最直接的方法是暴力枚举长度为m的串,然后 ...

  5. BZOJ 3864 Hero meet devil (状压DP)

    最近写状压写的有点多,什么LIS,LCSLIS,LCSLIS,LCS全都用状压写了-这道题就是一道状压LCSLCSLCS 题意 给出一个长度为n(n<=15)n(n<=15)n(n< ...

  6. bzoj3864: Hero meet devil

    Description There is an old country and the king fell in love with a devil. The devil always asks th ...

  7. DP套DP

    DP套DP,就是将内层DP的结果作为外层DP的状态进行DP的方法. [BZOJ3864]Hero meet devil 对做LCS的DP数组差分后状压,预处理出转移数组,然后直接转移即可. tr[S] ...

  8. bzoj 3864: Hero meet devil [dp套dp]

    3864: Hero meet devil 题意: 给你一个只由AGCT组成的字符串S (|S| ≤ 15),对于每个0 ≤ .. ≤ |S|,问 有多少个只由AGCT组成的长度为m(1 ≤ m ≤ ...

  9. [模板] dp套dp && bzoj5336: [TJOI2018]party

    Description Problem 5336. -- [TJOI2018]party Solution 神奇的dp套dp... 考虑lcs的转移方程: \[ lcs[i][j]=\begin{ca ...

随机推荐

  1. bitmap自己项目中处理遇到的问题

    String path = "图片路径";Bitmap bitmap = BitmapFactory.decodeFile(path);安卓处理图片都是Bitmap,然后取到图片的 ...

  2. Codeforces Gym 101194C Mr. Panda and Strips(2016 EC-Final,区间DP预处理 + 枚举剪枝)

    题目链接  2016 EC-Final 题意  现在要找到数列中连续两个子序列(没有公共部分).要求这两个子序列本身内部没有重复出现的数.   求这两个子序列的长度的和的最大值. 首先预处理一下.令$ ...

  3. 基于 OpenResty 的动态服务路由方案

    2019 年 5 月 11 日,OpenResty 社区联合又拍云,举办 OpenResty × Open Talk 全国巡回沙龙武汉站,又拍云首席布道师在活动上做了< 基于 OpenResty ...

  4. 洛谷——1508 Likecloud-吃、吃、吃

    题目背景 问世间,青春期为何物? 答曰:“甲亢,甲亢,再甲亢:挨饿,挨饿,再挨饿!” 题目描述 正处在某一特定时期之中的李大水牛由于消化系统比较发达,最近一直处在饥饿的状态中.某日上课,正当他饿得头昏 ...

  5. php中int类型在不同平台所占不同字节数理解

    1.在不同平台上占字节数与最大值 在32位平台上int占4个字节,在64位平台上int占8个字节,PHP_INT_SIZE 在32位平台上int的最大值2^31 - 1,在64位平台上int最大值2^ ...

  6. go语言学习之路六:接口详解

    Go语言没有类和继承的概念,但是接口的存在使得它可以实现很多面向对象的特性.接口定义了一些方法,但是这些方法不包含实现的代码.也就是说这些代码没有被实现(抽象的方法).同时接口里面也不包含变量. 看一 ...

  7. linux svn co 重新迁出

    在linux环境下,使用svn co (即svn checkout) 报svn: Authorization failed错误, 使用svn co svn://localhost/temp.cc /d ...

  8. centos7 samba安装与配置

    1.关闭防火墙. CentOS 7 是自带的firewall,CentOS 6 好像是iptables.关闭防火墙命令如下: 第一种方法是关闭防火墙: systemctl disable firewa ...

  9. LeakCanary: 让内存泄露无所遁形

    LeakCanary: 让内存泄露无所遁形 09 May 2015 本文为LeakCanary: Detect all memory leaks!的翻译.原文在: https://corner.squ ...

  10. DIY树莓派之随身工具箱

    摆弄树莓派有一年多了,在这里把经验分享给大家,少走弯路. 先放图两张. 搭建目的: wifi信号中转站\网站服务器\IC卡渗透测试\中间人\otr… 基于树莓派3 系统为Kali Linux 2017 ...