Time Limit: 8 Sec  Memory Limit: 128 MB
Submit: 397  Solved: 206
[Submit][Status][Discuss]

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

HINT

 

Source

首先想一下LCS的转移方程

$$lcs[i][j]=max \begin{cases} lcs[i-1][j-1]+1 & \text{if t[i]=s[j]} \\ lcs[i-1][j] \\ lcs[i][j-1] \end{cases}$$

这样的话,当$i$确定是,$lcs[i][j]$和$lcs[i][j-1]$最多相差$1$

且题目中说$|S|<= 15$,因此我们考虑把差分后的lcs数组状压起来

那么如何统计答案呢?

设$f[i][sta]$表示在第$i$个位置,此时lcs的状态为$sta$的方案数,

然后我们枚举一下这个位置选ACGT中的哪个

设$trans[sta'][A/C/G/T]$为在$sta$状态表示的lcs后加了ACGT中的一个后的状态,这个很显然可以预处理得到

那么转移方程为

$$f[i][ trans[sta][k] ] += f[i - 1][sta] $$

$$f[0][0] = 1$$

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN = , mod = 1e9 + ;
char S[], SS[] = {"ACGT"};
int a[], f[MAXN][( << ) + ], trans[( << ) + ][], N, Len, limit, ans[];
int tmp[][];
int solve(int sta, int ch) {
int ret = ;
memset(tmp, , sizeof(tmp));
for(int i = ; i < N; i++) tmp[][i + ] = tmp[][i] + ((sta >> i) & );
for(int i = ; i <= N; i++) {
int mx = ;
if(a[i] == ch) mx = tmp[][i - ] + ;
mx = max( max(mx, tmp[][i]), tmp[][i-]);
tmp[][i] = mx;
}
for(int i = ; i < N; i++) ret += ( << i) * (tmp[][i + ] - tmp[][i]);
return ret;
}
int main() {
#ifdef WIN32
freopen("a.in", "r", stdin);
#endif
int QWQ;scanf("%d", &QWQ);
while(QWQ--) {
memset(f, , sizeof(f));memset(ans, , sizeof(ans));
scanf("%s", S + );
N = strlen(S + ); limit = ( << N) - ;
for(int i = ; i <= N; i++)
for(int j = ; j < ; j++)
if(S[i] == SS[j]){a[i] = j + ;break;}
scanf("%d", &Len);
f[][] = ;
for(int sta = ; sta <= limit; sta++)
for(int j = ; j <= ; j++)
trans[sta][j] = solve(sta, j);
for(int i = ; i <= Len; i++)
for(int sta = ; sta <= limit; sta++)
for(int k = ; k <= ; k++)
f[i][ trans[sta][k] ] = (f[i][ trans[sta][k] ] + f[i - ][sta]) % mod;
for(int sta = ; sta <= limit; sta++)
ans[__builtin_popcount(sta)] = (ans[__builtin_popcount(sta)] + f[Len][sta]) % mod;
//这个函数是算出sta中1的个数
for(int i = ; i <= N; i++)
printf("%d\n", ans[i] % mod);
}
return ;
}

BZOJ3864: Hero meet devil(dp套dp)的更多相关文章

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

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

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

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

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

  4. BZOJ 3864 Hero meet devil (状压DP)

    最近写状压写的有点多,什么LIS,LCSLIS,LCSLIS,LCS全都用状压写了-这道题就是一道状压LCSLCSLCS 题意 给出一个长度为n(n<=15)n(n<=15)n(n< ...

  5. bzoj3864: Hero meet devil

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

  6. DP套DP

    DP套DP,就是将内层DP的结果作为外层DP的状态进行DP的方法. [BZOJ3864]Hero meet devil 对做LCS的DP数组差分后状压,预处理出转移数组,然后直接转移即可. tr[S] ...

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

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

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

  9. [模板] dp套dp && bzoj5336: [TJOI2018]party

    Description Problem 5336. -- [TJOI2018]party Solution 神奇的dp套dp... 考虑lcs的转移方程: \[ lcs[i][j]=\begin{ca ...

随机推荐

  1. 使用 WeihanLi.Npoi 操作 CSV

    使用 WeihanLi.Npoi 操作 CSV Intro 最近发现 csv 文件在很多情况下都在使用,而且经过大致了解,csv 格式简单,相比 excel 文件要小很多,读取也很是方便,而且也很通用 ...

  2. 【转贴】一次 JDBC 与 MySQL 因 “CST” 时区协商误解导致时间差了 14 或 13 小时的排错经历

    原文:https://juejin.im/post/5902e087da2f60005df05c3d ------------------------------------------------- ...

  3. Mysql 获取表设计查询语句

    SELECT COLUMN_NAME 列名, COLUMN_TYPE 数据类型, DATA_TYPE 字段类型, CHARACTER_MAXIMUM_LENGTH 长度, IS_NULLABLE 是否 ...

  4. Zabbix监控USG6300防火墙及交换机

    1.登录防火墙直接在web上面配置SNMP,只读团体名.读写团体名.Trap接收主机.安全名,点击应用完成防火墙上面的SNMP配置,如果你的命令行敲得6,可以使用命令行敲,配置效果一样,交换机没有这么 ...

  5. laravel部署创建新项目 助记

    打开进入homestead虚拟机,并进入code文件夹 cd ~/homestead && vagrant up && vagrant ssh cd ~/Code 进行 ...

  6. python + PyQt5 实现 简易计算器

    忽然想起之前一直想写个简单的计算器,今天就写了一下,界面有些简陋,但是基本功能实现没有问题 以下是源码: # --*-- coding:utf-8 --*-- import sys from PyQt ...

  7. HO6 Condo Insurance Policy

    The HO6 insurance Policy is the most common type of policy used to insure town homes and condos in t ...

  8. 【Python 08】汇率兑换2.0-1(字符串索引)

     1.案例描述 设计一个汇率换算程序,其功能是将人民币转换为美元,或者美元转换为人民币. 增加功能:根据输入判断是人民币还是美元,进行相应的转换计算. 2.案例分析 3.字符串 两个双引号或单引号括起 ...

  9. Git命令行管理代码、安装及使用

    出处:https://www.cnblogs.com/ximiaomiao/p/7140456.html Git安装和使用     目的:通过Git管理github托管项目代码 一.下载安装Git 1 ...

  10. 转://看懂Oracle中的执行计划

    一.什么是Oracle执行计划? 执行计划是一条查询语句在Oracle中的执行过程或访问路径的描述 二.怎样查看Oracle执行计划? 2.1 explain plan for命令查看执行计划 在sq ...