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:
 #include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=;
const int maxm=;
const int maxstate=;
const int mod=;
const char dna[]={'A','C','G','T'};
char s[maxn];
int T,n,m,lim,cnt[maxstate];
int cur[maxn],g[maxn],trans[maxstate][],f[][maxstate],ans[maxn];
void work(){
for (int sta=;sta<lim;sta++){
for (int i=;i<=n;i++) cur[i]=cur[i-]+((sta>>(i-))&);
for (int c=;c<;c++){
for (int i=;i<=n;i++) g[i]=;
for (int i=;i<=n;i++){
g[i]=max(g[i-],cur[i]);
if (s[i]==dna[c]) g[i]=max(g[i],cur[i-]+);
}
int res=;
for (int i=;i<=n;i++) if (g[i]>g[i-]) res|=(<<(i-));
trans[sta][c]=res;
}
}
memset(f[],,sizeof(f[]));
f[][]=;
for (int i=;i<=m;i++){
memset(f[i&],,sizeof(f[i&]));
for (int sta=;sta<lim;sta++) if (f[(i-)&][sta])
for (int c=;c<;c++) f[i&][trans[sta][c]]+=f[(i-)&][sta],f[i&][trans[sta][c]]%=mod;
}
memset(ans,,sizeof(ans));
for (int sta=;sta<lim;sta++) ans[cnt[sta]]+=f[m&][sta],ans[cnt[sta]]%=mod;
for (int i=;i<=n;i++) printf("%d\n",ans[i]);
}
int main(){
for (int i=;i<;i++) cnt[i]=cnt[i&(i-)]+;
for (scanf("%d",&T);T;T--) scanf("%s%d",s+,&m),n=strlen(s+),lim=<<n,work();
return ;
}

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. SQL string类型的数据按int类型排序 分类: SQL Server 2014-12-08 16:56 393人阅读 评论(0) 收藏

    说明: 我在做wms进销存软件时,发现一个问题:一张入库单(T_OutIn_BoxTop),入库扫描时要分成多箱,箱号(BoxTop_No)可以是数字也可以是字符串,所以箱号只能是字符串类型的,问题来 ...

  2. IE8下提示&#39;console&#39;没有定义错误

    在开发的过程中因为调试的原因,在代码中增加console.info("xxxx"),而未进行删除 在IE8下測试该代码所在的页面报错,例如以下: 须要注意的是,使用console对 ...

  3. HDU2255 奔小康赚大钱【二分图最佳匹配】

    题目链接: http://acm.hdu.edu.cn/showproblem.php? pid=2255 题目大意: 村里要分房子. 有N家老百姓,刚好有N间房子.考虑到每家都要有房住,每家必须分配 ...

  4. android 14 进度条和拖动条

    进度条: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:l ...

  5. android 12 click事件的不同实现方式

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layo ...

  6. 过滤掉html 标签

    /// <summary> /// 过滤掉html标签 /// </summary> /// <param name="Htmlstring"> ...

  7. Java_Activiti5_菜鸟也来学Activiti5工作流_之JUnit单元测试(四)

    /**ActivitiSpringJuinitTest.java * author : 冯孟活 ^_^ * dates : 2015年9月2日 下午2:16:54 * class : activiti ...

  8. 比较好的自学IT的网站

    其实这是我在知乎的一个回答,由于收藏人数众多,我想也许对有些初学者有用,故同步到Blog.此文章和知乎答案将不定期同步更新(知乎答案传送门). 入门与进阶: 学堂在线-最大的中文慕课(MOOC)平台学 ...

  9. linux教程:配置Tomcat开机启动

    我们在linux下安装好tomcat之后:经常是需要配置到开机启动的: 这样的话就不需要我们每次重启linux服务器之后自己在登陆运行startup.sh文件启动tomcat了 本次的演示环境是在ce ...

  10. 内网映射到公网工具 --- ngrok

    ngrok可以将内网映射到公网上,这样就可以在公网上访问你的网络服务. 该工具通常在进行app开发和微信开发时比较有用,这样就可避免在公网服务器上单独部署项目,通过映射,直接连接本地服务即可进行开发. ...