【BZOJ3864】Hero meet devil

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串的最长公共子序列长度为1...|S|的串的个数。

题解:话说这种DP套DP的题最近有点流行~

还记得怎么求最长公共子序列吗?记得那个求最长公共子序列时的矩阵吗?不记得我就再说一遍。

令f[i][j]表示T串中到了第i个数,S串中到了第j个数,的LCS的长度。那么经典的DP方程:

$f[i][j]=max(f[i-1][j],f[i][j-1],(T[i]==S[j])?(f[i-1][j-1]+1):0)$

好了,但是我们求的是方案数,如果直接这样DP的话,需要记录的状态非常多(当前T可能的字符,之前T可能的字符。。。)。但是我们发现S的长度非常小,可以考虑把它单独拿出来处理一下。

因为每一行只能从上一行转移过来,我们不妨状压所有可能的行,暴力计算出在T中添加一个字符后会转移到哪个行。但是行中每一位的数不是0/1,差分一下就好了。处理出所有的转移后,再跑一个DP统计答案就行了。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstring>
using namespace std;
const int mod=1000000007;
int n,m;
int to[1<<16][4],s[20],cnt[1<<16],f[2][1<<16],ans[20];
char str[20];
void init()
{
memset(to,0,sizeof(to));
memset(cnt,0,sizeof(cnt));
memset(f,0,sizeof(f));
memset(ans,0,sizeof(ans));
int i,j,k,s1,s2,t1,t2,tar;
for(i=0;i<(1<<n);i++)
{
if(i) cnt[i]=cnt[i-(i&-i)]+1;
for(j=0;j<4;j++)
{
for(tar=s1=s2=t1=t2=0,k=0;k<n;k++)
{
t1=s1,t2=s2,s2+=((i>>k)&1),s1=max(t1,s2);
if(s[k]==j) s1=max(s1,t2+1);
tar|=((s1-t1)<<k);
}
to[i][j]=tar;
}
}
}
void work()
{
scanf("%s%d",str,&m),n=strlen(str);
int i,j;
for(i=0;i<n;i++)
{
if(str[i]=='A') s[i]=0;
if(str[i]=='G') s[i]=1;
if(str[i]=='C') s[i]=2;
if(str[i]=='T') s[i]=3;
}
init();
f[0][0]=1;
for(j=0;j<=m;j++)
{
for(i=0;i<(1<<n);i++) f[(j&1)^1][i]=0;
for(i=0;i<(1<<n);i++) f[(j&1^1)][to[i][0]]=(f[j&1^1][to[i][0]]+f[j&1][i])%mod,f[(j&1^1)][to[i][1]]=(f[j&1^1][to[i][1]]+f[j&1][i])%mod,f[(j&1^1)][to[i][2]]=(f[j&1^1][to[i][2]]+f[j&1][i])%mod,f[(j&1^1)][to[i][3]]=(f[j&1^1][to[i][3]]+f[j&1][i])%mod;
}
for(i=0;i<(1<<n);i++) ans[cnt[i]]=(ans[cnt[i]]+f[m&1][i])%mod;
for(i=0;i<=n;i++) printf("%d\n",ans[i]);
}
int main()
{
int T;
scanf("%d",&T);
while(T--) work();
return 0;
}

【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. BZOJ3864: Hero meet devil(dp套dp)

    Time Limit: 8 Sec  Memory Limit: 128 MBSubmit: 397  Solved: 206[Submit][Status][Discuss] Description ...

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

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

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

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

  6. bzoj3864: Hero meet devil

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

  7. DP套DP

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

  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. 使用PyQt4制作一个正则表达式测试小工具

    最近在做一些网络爬虫的时候,会经常用到正则表达式.为了写出正确的正则表达式,我经常在这个网站上进行测试:Regex Tester.这个页面上面一个输入框输入正则表达式,下面一个输入框输入测试数据,上面 ...

  2. 【转载】51CTO-Android设置模拟器屏幕大小

    在Eclipse Android中设置模拟器屏幕大小是本文要介绍的内容,主要是来了解并学习Eclipse Android中模拟器的设置,具体关于Eclipse Android内容的详解来看本文.   ...

  3. EOJ 3.30 A. 打工时不可能打工的【贪心】

    [链接]:https://acm.ecnu.edu.cn/contest/59/problem/A/ A. 打工时不可能打工的 Time limit per test: 2.0 seconds Mem ...

  4. BZOJ1054(搜索)

    大力搜,状态用一个16位的数字表示. #include <bits/stdc++.h> using namespace std; #define rep(i,a,b) for(int i( ...

  5. 常用的Linux系统命令

    一.linux简介                                                                                            ...

  6. CSS定位与布局:浮动

    浮动的特点   浮动(float)属性提出的作用是实现文字的环绕效果,一个元素浮动后,会脱离普通流.主要的特点如下: 浮动的元素会向左或者向右移动直到它的外边缘接触容器框(containing blo ...

  7. Windows 10系统出现:“出现系统还原使用的卷影复制服务无法运行...”的问题解决

    在服务中是:Volume Shadow Copy和Microsoft Software Shadow Copy Provider这两项.把它开启.

  8. React 入门之路

    React React简介 是由Facebook公司推广的一套框架,已经应用instagram等产品 React就是为了提供应用程序性能而设计的一套框架 在angular中,对dom提供了一些指令,让 ...

  9. 【2019】问题记录一:后端获取URL参数的值内加号“+”变成空格“ ”

    问题:URL参数中加号“+”变成空格“ ” 一.现象     URL如:http://example.****.com/controller/action?param=rice+cook+panda ...

  10. 由ASIHttpRequest里的block引发的思考

    项目发http请求,现在一般的都是用的第三方开源库,当然发异步请求时我们也会写几个回调函数来进行请求返回时的处理.不过前段时间看一个朋友写的代码,里面很用block简单的实现了回调相关的部分.比如: ...