【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. ie8实现无刷新文件上传

    ie8由于无法使用FormData,想要无刷新上传文件就显得比较麻烦.这里推荐使用jQuery-File-Upload插件,它能够很方便的解决ie8无刷新文件上传问题.(最低兼容到ie6) jQuer ...

  2. 计蒜客 ACM竞赛高校联盟训练赛 第8场 煎牛排

    水一水. https://nanti.jisuanke.com/t/24205 煎牛排 题目描述 又是一个难得的周六,是时候远离食堂和外卖出去大吃一顿了.圈内知名吃货AA正想着中午去吃汉堡炸鸡烤肉火锅 ...

  3. Codeforces 371E Subway Innovation (前缀和预处理应用)

    题目链接 Subway Innovation 首先不难想到所求的k个点一定是连续的,那么假设先选最前面的k个点,然后在O(1)内判断第2个点到第k+1个点这k个点哪个更优. 判断的时候用detla[i ...

  4. CodeForces - 16E Fish

    Discription n fish, numbered from 1 to n, live in a lake. Every day right one pair of fish meet, and ...

  5. 通信API、使用Web Workers处理线程

    1.跨文档消息传输                                               要想接受从其他的窗口那里发过来的消息,就必须对窗口对象的message事件进行监视. w ...

  6. html中常用的标签小结

    内容详细标签: <h1>~<h6>标题标签<pre>格式化文本<u>下划线(underline)<i>斜体字(italics)<cit ...

  7. 修改xampp默认sql密码

    1 登录localhost/phpmyadmin 点击权限修改 修改root@localhost下的密码并执行 2 刷新页面 找到xampp文件夹下的phpMyAdmin文件夹中的config.inc ...

  8. MFC中 CString与int的转化

    int 转化为SCtring: int n = 123; CString str; str.Format("%d",n); 报错的话则改为:str.Format(_T(" ...

  9. ylb:SQL 表的高级查询-多表连接和子查询

    ylbtech-SQL Server: SQL Server-表的高级查询-多表连接和子查询 SQL Server 表的高级查询-多表连接和子查询. 1,ylb:表的高级查询-多表连接和子查询 返回顶 ...

  10. CLI/C++中混合类的使用【转】

    http://www.cppblog.com/mzty/archive/2007/12/24/39517.html CLI/C++中混合类的使用 一 混合类 所谓混合类是指CLI/C++中native ...