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. bzoj1827 [Usaco2010 Mar]gather 奶牛大集会

    不就是移一下树根,回溯一下吗? 诶?黄学长为什么可以直接找? 诶?这不是重心吗? YY了一下证明 很简单 由于重心max{sz[v]} <= sz[u] / 2的性质,可以证明每一步远离重心的移 ...

  2. php开发工具zendstudio13破解补丁

    io?  Intelligent Code Editor Robust Debugging Capabilities Eclipse Ecosystem Mobile: AngularJS, Ioni ...

  3. 《大话操作系统——做坚实的project实践派》(6)

     继续写硬件体系.这个不写完.不会写操作系统内核.由于根基不正,则难于达到上层境地.

  4. lua字符匹配

    匹配下列格式的数据中的 source和MAC地址: Chain WiFiDog_br-lan_Outgoing (1 references) pkts bytes target prot opt in ...

  5. 怎样让HTML5调用手机摄像头拍照——实践就是一切

    原文:怎样让HTML5调用手机摄像头拍照--实践就是一切 NanShan 小编将思路提供给了大家.学编程最重要的是实践,我这尽管有完好的代码,可是希望大家都能够自己写出属于自己的代码 HTML5 Th ...

  6. InstallShield 12 制作安装包

    目  录 一. 二. 三. (一) 打开project... 2 (二) project助手页面... 3 1.Application Information:程序信息... 4 2.Installa ...

  7. PHP面向对象之旅:模板模式(转)

    抽象类的应用就是典型的模版模式 抽象类的应用就是典型的模版模式,先声明一个不能被实例化的模版,在子类中去依照模版实现具体的应用. 我们写这样一个应用: 银行计算利息,都是利率乘以本金和存款时间,但各种 ...

  8. spring aop 如何切面到mvc 的controller--转载

    原文:http://yjian84.iteye.com/blog/1920787 网上搜罗半天,不知道什么原因,看了源码,好像他们说的controller 是不受代理的,也对哈,不知道怎么办,于是在h ...

  9. ipad itunes 恢复

    http://jingyan.baidu.com/article/a3aad71aa58efbb1fa00967c.html http://act.feng.com/wetools/index.php ...

  10. oracle中的层级递归查询操作

    oracle中的层级操作非常方便,在使用之后爱不释手,以前要实现该种数据查询操作,需要非常复杂的实现过程.在oracle中通过connect by可以实现前面的目的,通常情况下层级查询基本都能实现递归 ...