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最长公共子序列是\([0,|s|]\)的串有多少个

思路

dp of dp

因为最长公共子序列的dp矩阵每一行相邻两项不会超过1,所以就可以用二进制状压

然后就先dp出每个状态加上一个字符的转移

然后上状压dp就可以了

注意dp数组的清零问题


#include<bits/stdc++.h>

using namespace std;

const int Mod = 1e9 + 7;
const int N = 16;
const int M = 1 << N; int trans[M][4], bitcnt[M], dp[2][M];
int len, m, ans[N], f[N], g[N];
char s[N]; int add(int a, int b) {
return (a += b) >= Mod ? a - Mod : a;
} void solve() {
scanf("%s%d", s + 1, &m);
len = strlen(s + 1);
for (int i = 1; i <= len; i++) {
if (s[i] == 'A') s[i] = 0;
if (s[i] == 'C') s[i] = 1;
if (s[i] == 'G') s[i] = 2;
if (s[i] == 'T') s[i] = 3;
}
for (int i = 0; i < (1 << len); i++) {
for (int j = 1; j <= len; j++) {
f[j] = f[j - 1] + ((i >> (j - 1)) & 1);
}
for (int j = 0; j < 4; j++) {
for (int k = 1; k <= len; k++) {
g[k] = max(g[k - 1], f[k]);
if (s[k] == j) g[k] = max(g[k], f[k - 1] + 1);
}
trans[i][j] = 0;
for (int k = 1; k <= len; k++) {
trans[i][j] |= (g[k] - g[k - 1]) << (k - 1);
}
}
}
int ind = 0;
for (int i = 0; i < (1 << len); i++) dp[ind][i] = 0; // **
dp[ind][0] = 1;
for (int i = 1; i <= m; i++) {
ind ^= 1;
for (int j = 0; j < (1 << len); j++) {
dp[ind][j] = 0;
}
for (int j = 0; j < (1 << len); j++) if (dp[ind ^ 1][j]) {
for (int k = 0; k < 4; k++) {
dp[ind][trans[j][k]] = add(dp[ind][trans[j][k]], dp[ind ^ 1][j]);
}
}
}
for (int i = 0; i <= len; i++) ans[i] = 0;
for (int i = 0; i < (1 << len); i++) {
ans[bitcnt[i]] = add(ans[bitcnt[i]], dp[ind][i]);
}
for (int i = 0; i <= len; i++) {
printf("%d\n", ans[i]);
}
} int main() {
#ifdef dream_maker
freopen("input.txt", "r", stdin);
#endif
for (int i = 0; i < M; i++) {
for (int j = 1; j <= N; j++) {
bitcnt[i] += (i >> (j - 1)) & 1;
}
}
int T; scanf("%d", &T);
while (T--) solve();
return 0;
}

BZOJ3864: Hero meet devil【dp of dp】的更多相关文章

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

  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. bzoj3864: Hero meet devil

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

  5. 【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 ...

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

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

  7. DP套DP HDOJ 4899 Hero meet devil(国王的子民的DNA)

    题目链接 题意: 给n长度的S串,对于0<=i<=|S|,有多少个长度为m的T串,使得LCS(S,T) = i. 思路: 理解的不是很透彻,先占个坑. #include <bits/ ...

  8. bzoj 3864: Hero meet devil(dp套dp)

    题面 给你一个只由\(AGCT\)组成的字符串\(S (|S| ≤ 15)\),对于每个\(0 ≤ .. ≤ |S|\),问 有多少个只由\(AGCT\)组成的长度为\(m(1 ≤ m ≤ 1000) ...

  9. bzoj 3864: Hero meet devil

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

随机推荐

  1. EM算法及其推广

    概述 EM算法是一种迭代算法,用于含有隐变量(hidden variable)的概率模型参数的极大似然估计,或极大后验概率估计. EM算法的每次迭代由两步组成:E步,求期望(expectation): ...

  2. hdu 5724-Chess(状态压缩+sg函数)

    hdu 5724-Chess 代码: #include<bits/stdc++.h> using namespace std; ; <<N]; bool S[N]; void ...

  3. 2017"百度之星"程序设计大赛 - 资格赛-度度熊与邪恶大魔王(dp+后缀最小值)

    度度熊与邪恶大魔王 思路:由于防御和血量的范围很小,所以暴力枚举出对于每种防御造成的每种伤害所需的最小花费,最后只需在伤害大于等于血量的情况下再找到最小花费(这个只需要后缀最小值预处理一下就可以了) ...

  4. js中文乱码

    js中文乱码 我的页面是uft-8,处理中文还是乱码, 所在在处理页面增加了 request.setCharsetEncoding("UFT-8"); ////////////// ...

  5. java学习笔记 --- 集合(续)

    1.map集合 1.1.特点:将键映射到值的对象.一个映射不能包含重复的键:每个键最多只能映射到一个值. 1.2.Map集合和Collection集合的区别? Map集合存储元素是成对出现的,Map集 ...

  6. L1-037 A除以B

    真的是简单题哈 —— 给定两个绝对值不超过100的整数A和B,要求你按照“A/B=商”的格式输出结果. 输入格式: 输入在第一行给出两个整数A和B(−100≤A,B≤100),数字间以空格分隔. 输出 ...

  7. include指令和include动作有什么区别?

    include指令         称为文件加载指令,可以将其他的文件插入jsp网页,被插入的文件必须保证插入后形成的新文件符合jsp页面的语法规则. include指令语法格式:<%@incl ...

  8. centos7 mysql的安装

    本文记录centos7安装mysql的一些过程与遇到的一些坑 下载mysql的压缩包,直接从官网上面下载,链接:http://dev.mysql.com/downloads/mysql/ 选择 MyS ...

  9. jQuery的noConflict以及插件扩展

    一.noConflict函数 JavaScript有很多插件,如果jQuery对象的$与其他插件冲突,我们可以使用noConflict()方法去掉$或者使用其他的符号代替 注:noConflict() ...

  10. myecilpse +TOMCAT+web:jsp向mysql添加数据,查询在jsp页面显示

    <%@ page language="java" import="java.util.*" import="com.mysql.jdbc.Dri ...