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
 
题解:
我们先考虑给定T和S,如何去求他们的lcs
显然我们会设g[i][j]表示T[1..i]和S[1..j]的lcs
所以g[i][j]=max(g[i-1][j],g[i][j-1],(g[i-1][j-1]+1)*(T[i]==S[j]))
我们发现g数组有两个性质
1.g[i][j]只和上一行和这一行有关
2.对于任意的i,j,有g[i][j]-g[i][j-1]=0或1
而且这题的|S|=15,这启示我们可以将j那一维状态压缩一下,用一个二进制数存储相邻两项的差值即可
设trans[sta][c]表示T已经匹配了若干位的状态为sta,假如下一位为c,状态会变为trans[sta][c]
这个可以O(n*2n)预处理出来
然后在设f[i][sta]表示T已经匹配到了i位,状态为sta的方案数,利用一下trans数组进行转移(记得要滚动数组),这个复杂度为O(m*2n)
code:
  1. #include<cstdio>
  2. #include<iostream>
  3. #include<cmath>
  4. #include<cstring>
  5. #include<algorithm>
  6. using namespace std;
  7. const int maxn=;
  8. const int maxm=;
  9. const int maxstate=;
  10. const int mod=;
  11. const char dna[]={'A','C','G','T'};
  12. char s[maxn];
  13. int T,n,m,lim,cnt[maxstate];
  14. int cur[maxn],g[maxn],trans[maxstate][],f[][maxstate],ans[maxn];
  15. void work(){
  16. for (int sta=;sta<lim;sta++){
  17. for (int i=;i<=n;i++) cur[i]=cur[i-]+((sta>>(i-))&);
  18. for (int c=;c<;c++){
  19. for (int i=;i<=n;i++) g[i]=;
  20. for (int i=;i<=n;i++){
  21. g[i]=max(g[i-],cur[i]);
  22. if (s[i]==dna[c]) g[i]=max(g[i],cur[i-]+);
  23. }
  24. int res=;
  25. for (int i=;i<=n;i++) if (g[i]>g[i-]) res|=(<<(i-));
  26. trans[sta][c]=res;
  27. }
  28. }
  29. memset(f[],,sizeof(f[]));
  30. f[][]=;
  31. for (int i=;i<=m;i++){
  32. memset(f[i&],,sizeof(f[i&]));
  33. for (int sta=;sta<lim;sta++) if (f[(i-)&][sta])
  34. for (int c=;c<;c++) f[i&][trans[sta][c]]+=f[(i-)&][sta],f[i&][trans[sta][c]]%=mod;
  35. }
  36. memset(ans,,sizeof(ans));
  37. for (int sta=;sta<lim;sta++) ans[cnt[sta]]+=f[m&][sta],ans[cnt[sta]]%=mod;
  38. for (int i=;i<=n;i++) printf("%d\n",ans[i]);
  39. }
  40. int main(){
  41. for (int i=;i<;i++) cnt[i]=cnt[i&(i-)]+;
  42. for (scanf("%d",&T);T;T--) scanf("%s%d",s+,&m),n=strlen(s+),lim=<<n,work();
  43. return ;
  44. }

bzoj3864: Hero meet devil的更多相关文章

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

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

  2. 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 ...

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

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

  4. 【BZOJ3864】Hero meet devil DP套DP

    [BZOJ3864]Hero meet devil Description There is an old country and the king fell in love with a devil ...

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

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

  6. BZOJ3864 & HDU4899:Hero meet devil——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=3864 http://acm.hdu.edu.cn/showproblem.php?pid=4899 ...

  7. HDU 4899 Hero meet devil(状压DP)(2014 Multi-University Training Contest 4)

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

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

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

  9. bzoj 3864: Hero meet devil

    bzoj3864次元联通们 第一次写dp of dp (:з」∠) 不能再颓废啦 考虑最长匹配序列匹配书转移 由于dp[i][j]的转移可由上一行dp[i-1][j-1],dp[i-1][j],dp[ ...

随机推荐

  1. 320. Generalized Abbreviation

    首先想到的是DFS,对于每个单词的字母都遍历,比如 spy: 1py,s1y,sp1 然后每个遍历完的单词再DFS..左右有数字就合并比如 1py: 11y=>2py, 1p1 这样.. 但是单 ...

  2. Struts2和Struts1的不同

    转载(没看懂) Action 类 ◆Struts1要求Action类继承一个抽象基类org.apache.struts.action.Action.Struts1的一个普遍问题是使用抽象类编程而不是接 ...

  3. 10.24 noip模拟试题

    尼玛pdf依旧不会粘23333 /* 每段合并到总的里面 假设总的有X个 这一段有Y个 一共有X+1个空 那么就有 C(X+1,1)+C(X+1,2)+C(X+1,3)+...+C(X+1,Y) 这样 ...

  4. excel中VBA对多个文件的操作

    添加引用 "Scripting.FileSystemObject" (Microsoft Scripting Runtime) '用于操作文件.目录 Sub 数据整理部分() ' ...

  5. oo面向对象原则

    1.单一职责原则 一个类,最好只做一件事,只有一个引起他变化的原因否则就应该考虑重构. 2.开放封闭原则 软件实体应该是可扩展的,而不是可修改的.也就是说对扩展开放,对修改封闭.主要体现在两个方面: ...

  6. angularjs modal 嵌套modal的问题

    anguarjs中当遇到modal嵌套modal的时候,比如一个modal弹出啦一个modal1,关闭modal1后,modal本身的关闭功能失效,那么需要$modal来生命弹出的modal1并且关闭 ...

  7. 微软企业库Microsoft Enterprise Library的相关文章链接

    微软企业库4.1学习笔记 http://blog.csdn.net/anyqu/article/category/1228691/3 黄聪:Enterprise Library 5.0 系列教程 ww ...

  8. JS中escape 方法和C#中的对应

    在项目中遇到js中escape过的json字符串,需要在C#中对应模拟编码,记得原来遇到过这个问题,但是当时没记录下来方案, 于是又搜索了一番,发现别人说的都是HttpUtility.UrlEncod ...

  9. Ubuntu系统中安装RPM格式包的方法

    Ubuntu的软件包格式为deb,而RPM格式的包则是Red Hat 相关系统所用的软件包.当我们看到一个想用的软件包时,如果他是RPM格式,而你的操作系统是Ubuntu,那岂不是很遗憾?其实,在Ub ...

  10. Tensor神经网络进行知识库推理

    本文是我关于论文<Reasoning With Neural Tensor Networks for Knowledge Base Completion>的学习笔记. 一.算法简介 网络的 ...