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. Java新知识系列 四

    []URL的组成<协议>://<主机>:<端口>/<路径> . []线程的定义实例化和启动. []类的final变量初始化需要满足的条件. []管道通信 ...

  2. MongoDB 提升性能的18原则(开发设计阶段)

    MongoDB 是高性能数据,但是在使用的过程中,大家偶尔还会碰到一些性能问题.MongoDB和其它关系型数据库相比,例如 SQL Server .MySQL .Oracle 相比来说,相对较新,很多 ...

  3. c/c++ linux epoll系列1 创建epoll

    linux epoll系列1 创建epoll 据说select和poll的弱点是,随着连接(socket)的增加,性能会直线下降. epoll不会随着连接(socket)的增加,性能直线下降. 知识点 ...

  4. Serverless架构

    什么是Serverless架构 Servlerless 架构是新兴的架构体系,在Serverless 架构中,开发者无需考虑服务器的问题,计算资源作为服务而不是服务器的概念出现,这样,开发者只需要关注 ...

  5. 为什么不使用github的wiki而是使用mkdocs做文档管理?

    为什么不使用github的wiki而是使用mkdocs做文档管理? 目前 KSFramework 是使用mkdocs来做在线文档 而非使用github的wiki,这是为什么呢? 在windows下搭建 ...

  6. 滑动窗口最大值的golang实现

    给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧.你只可以看到在滑动窗口 k 内的数字.滑动窗口每次只向右移动一位. 返回滑动窗口最大值 输入: nums = [, ...

  7. web Deploy发布问题

    使用vs开发的时候,经常会发布项目.传统发布是登陆远程桌面.或ftp这些发布都有一定的麻烦.不能灵活的管理发布的文件.因此后来研究了web Deploy,研究之后发现是很不错的发布工具.这里把我使用w ...

  8. vs 2015安装包

    Visual Studio 2015 下载含(更新3)及密钥 Visual Studio 2015 是一个丰富的集成开发环境,可用于创建出色的 Windows.Android 和 iOS 应用程序以及 ...

  9. Shiro学习(一)——Shiro简介

    Apache Shiro是Java的一个安全框架.目前,使用Apache Shiro的人越来越多,因为它相当简单,对比Spring Security,可能没有Spring Security做的功能强大 ...

  10. 2017-8-2新开了一个ABP交流的QQ群(291304962 ),欢迎加入

    因为ABP架构设计交流群人数一直爆满,很多想交流ABP的朋友无法加进群里, 刚新建了一个QQ群,群号291304962(ABP架构设计交流群3),欢迎对ABP感兴趣的朋友加入. 欢迎加QQ群: ABP ...