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. 使用Ajax轮询模拟简单的站内信箱(消息管理)功能

    前一段时间项目需要写一个类似于站内信箱的消息管理的功能,由于对前端不是很熟悉,刚开始不知道怎么做,后来看了网上的方案,现模拟一个非常简单的消息管理. 我们首先看一下最终效果的样式,就是非常简单的一个样 ...

  2. c/c++ 重载运算符 关系,下标,递增减,成员访问的重载

    重载运算符 关系,下标,递增减,成员访问的重载 为了演示关系,下标,递增减,成员访问的重载,创建了下面2个类. 1,类StrBlob重载了关系,下标运算符 2,类StrBlobPtr重载了递增,抵减, ...

  3. JavaScript -- 原型:prototype的使用

    JavaScript -- 原型:prototype的使用 在 JavaScript 中,prototype 是函数的一个属性,同时也是由构造函数创建的对象的一个属性. 函数的原型为对象. 它主要在函 ...

  4. SQLServer之创建主XML索引

    创建主XML索引注意事项 若要创建主 XML 索引,请使用 CREATE INDEX (Transact-SQL) Transact-SQL DDL 语句. XML 索引不完全支持可用于非 XML 索 ...

  5. linux 配置vim(vimrc)

    打开终端:ctrl+alt+t 进入vim文件:cd /etc/vim 打开vimrc文件:sudo gedit vimrc 然后在行末if语句前加上下面的内容,"  这个符号为注释,后面内 ...

  6. Kafka 0.11.0.0 实现 producer的Exactly-once 语义(官方DEMO)

    <dependency> <groupId>org.apache.kafka</groupId> <artifactId>kafka-clients&l ...

  7. git添加/删除远程仓库

    注意:仓库只有管理员建的你才有权限上传,不然自己建的也没用,没权限上传 1.远程仓库路径查询 git remote -v 2.添加远程仓库 git remote add origin <你的项目 ...

  8. hbase 迁库移库步骤

    1 将数据导出 hbase org.apache.hadoop.hbase.mapreduce.Export t_zyzx_grzyfwtjxxb /hbase/data_backup/2018103 ...

  9. centos7下kubernetes(11。kubernetes-运行一次性任务)

    容器按照持续运行的时间可以分为两类:服务类容器和工作类容器 服务类容器:持续提供服务 工作类容器:一次性任务,处理完后容器就退出 Deployment,replicaset和daemonset都用于管 ...

  10. error C2381: “exit”: 重定义;__declspec(noreturn) 不同

    问题: error C2381: “exit” : 重定义:__declspec(noreturn) 不同 解决办法: 调换一下头文件的包含次序: #include <GL/glut.h> ...