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. PHP——四种基本排序算法

    分别用冒泡排序法,快速排序法,选择排序法,插入排序法将下面数组中的值按照从小到大的顺序进行排序. $arr(1,43,54,62,21,66,32,78,36,76,39); 1. 冒泡排序 思路分析 ...

  2. Javascript UserAgent 获取平台及浏览器信息

    公司最近要做一套类似于百度统计的功能,于是收集整理了其中根据UserAgent获取客户端平台及浏览器信息的资料,不仅仅试用于Javascript,其他语言也可以参考相应正则进行改进.当然,肯定有不足的 ...

  3. JSNI GWT中的东东

    二.JavaScript Native InterfaceJavaScript本地接口JSNI.1)声明一个本地方法在JSNI中声明一个本地方法时,使用Java的标准native关键字,就像在JNI( ...

  4. 基于Lucene3.5.0怎样从TokenStream获得Token

    通过学习Lucene3.5.0的doc文档,对不同release版本号 lucene版本号的API修改做分析.最后找到了有价值的修改信息. LUCENE-2302: Deprecated TermAt ...

  5. Merge into的使用具体解释-你Merge了没有

    Merge是一个很实用的功能,相似于Mysql里的insert into on duplicate key. Oracle在9i引入了merge命令,  通过这个merge你可以在一个SQL语句中对一 ...

  6. 利用 SysRq 键排除和诊断系统故障

    说白了,SysRq手动触发而不用命令, /proc/sysrq-trigger 这个是通过命令接口 实验:LINUX窗口下 ,CTRL+ALT+F1,切换到TTY1,在文本模式下,按下 ALT+Sys ...

  7. 初步掌握Yarn的架构及原理

    1.YARN 是什么? 从业界使用分布式系统的变化趋势和 hadoop 框架的长远发展来看,MapReduce的 JobTracker/TaskTracker 机制需要大规模的调整来修复它在可扩展性, ...

  8. Android(java)学习笔记208:Android中操作JSON数据(Json和Jsonarray)

    1.Json 和 Xml       JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. JSON采用完全独立于语言的 ...

  9. Linux软件安装,RPM与YUM

    为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/SJQ. http://www.cnblogs.com/shijiaqi1066/p/3843955.html ...

  10. rabbitmq pika connection closed

    You are here: Home / rabbitmq pika connection closed rabbitmq pika connection closed By lijiejie on  ...