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. Linux 安装 powershell

    linux 安装 powershell Intro powershell 已经推出了一个 Powershell Core, 版本号对应 Powershell 6.x,可以跨平台,支持 Linux 和 ...

  2. Ext.grid.panel 改变某一行的字体颜色

    grid.getStore().addListener('load', handleGridLoadEvent); function handleGridLoadEvent(store, record ...

  3. Spring 事务与脏读、不可重复读、幻读

    索引: 目录索引 参看代码 GitHub: 1.Spring 事务 2.事务行为 一.Spring 事务: Spring 的事务机制是用统一的机制来处理不同数据访问技术的事务处理. Spring 的事 ...

  4. [转] Linux Asynchronous I/O Explained

    Linux Asynchronous I/O Explained (Last updated: 13 Apr 2012) *************************************** ...

  5. Docker之进入容器(三)

    1.简介 经过前面两篇博客的扫盲,大家多多少少对docker有了一个基本的了解,也接触了docker的常用命令.在这篇博客中,我将介绍进入docker容器的几种方式. 2.进入docker中的几种方式 ...

  6. LeetCode算法题-Relative Ranks(Java实现)

    这是悦乐书的第248次更新,第261篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第115题(顺位题号是506).根据N名运动员的得分,找到他们的相对等级和得分最高的三个 ...

  7. 在android中进行单元测试的步骤

    若不知道怎么配上面两个参数 

  8. Vector与ArrayList区别

    1)Vector的方法都是同步的(Synchronized),是线程安全的: ArrayList的方法是线程不安全的. 由于线程同步必然会影响性能,因此,ArrayList的性能比Vector好. 请 ...

  9. vue-router query 传对象需要JSON.stringify()转化

    先说一下场景-微信公众号网页开发中,一个文章列表点击跳转详情页.代码如下 1 2 3 this.$router.push({path: '/wx/detail', query: {res: data} ...

  10. 文本分类实战(四)—— Bi-LSTM模型

    1 大纲概述 文本分类这个系列将会有十篇左右,包括基于word2vec预训练的文本分类,与及基于最新的预训练模型(ELMo,BERT等)的文本分类.总共有以下系列: word2vec预训练词向量 te ...