BZOJ3864: Hero meet devil(dp套dp)
Time Limit: 8 Sec Memory Limit: 128 MB
Submit: 397 Solved: 206
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
GTC
10
Sample Output
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)的更多相关文章
- 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 ...
- bzoj千题计划241:bzoj3864: Hero meet devil
http://www.lydsy.com/JudgeOnline/problem.php?id=3864 题意: 给你一个DNA序列,求有多少个长度为m的DNA序列和给定序列的LCS为0,1,2... ...
- HDU 4899 Hero meet devil (状压DP, DP预处理)
题意:给你一个基因序列s(只有A,T,C,G四个字符,假设长度为n),问长度为m的基因序列s1中与给定的基因序列LCS是0,1......n的有多少个? 思路:最直接的方法是暴力枚举长度为m的串,然后 ...
- BZOJ 3864 Hero meet devil (状压DP)
最近写状压写的有点多,什么LIS,LCSLIS,LCSLIS,LCS全都用状压写了-这道题就是一道状压LCSLCSLCS 题意 给出一个长度为n(n<=15)n(n<=15)n(n< ...
- bzoj3864: Hero meet devil
Description There is an old country and the king fell in love with a devil. The devil always asks th ...
- DP套DP
DP套DP,就是将内层DP的结果作为外层DP的状态进行DP的方法. [BZOJ3864]Hero meet devil 对做LCS的DP数组差分后状压,预处理出转移数组,然后直接转移即可. tr[S] ...
- 【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 ...
- bzoj 3864: Hero meet devil [dp套dp]
3864: Hero meet devil 题意: 给你一个只由AGCT组成的字符串S (|S| ≤ 15),对于每个0 ≤ .. ≤ |S|,问 有多少个只由AGCT组成的长度为m(1 ≤ m ≤ ...
- [模板] dp套dp && bzoj5336: [TJOI2018]party
Description Problem 5336. -- [TJOI2018]party Solution 神奇的dp套dp... 考虑lcs的转移方程: \[ lcs[i][j]=\begin{ca ...
随机推荐
- 设计模式系列之单例模式(Singleton Pattern)
单例模式(Singleton Pattern)是 Java 中最简单的设计模式之一.这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式.这种模式涉及到一个单一的类,该类负责创建自己的对象 ...
- Python使用Plotly绘图工具,绘制柱状图
使用Plotly绘制基本的柱状图,需要用到的函数是graph_objs 中 Bar函数 通过参数,可以设置柱状图的样式. 通过barmod进行设置可以绘制出不同类型的柱状图出来. 我们先来实现一个简单 ...
- Linux中VSFTP的配置
配置VSFTP服务器: 1.安装VSFTP,可以参考Linux 中yum的配置来安装: yum installvsftpd.x86_64 -y 2.修改SELinux: setenforce 0 查看 ...
- Undefined attribute name (placeholder)
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- Cs231n课堂内容记录-Lecture 5 卷积神经网络介绍
Lecture 5 CNN 课堂笔记参见:https://zhuanlan.zhihu.com/p/22038289?refer=intelligentunit 不错的总结笔记:https://blo ...
- Cs231n-assignment 1作业笔记
KNN assignment1 KNN讲解参见: https://blog.csdn.net/u014485485/article/details/79433514?utm_source=blogxg ...
- Ubuntu composer 安装thinkphp5 失败,报错:[ErrorException] mkdir(): Permission denied
在Linux环境下,使用composer安装thinkphp5,安装时,报错:[ErrorException] mkdir(): Permission denied : 看 ...
- LeetCode算法题-Non-decreasing Array(Java实现)
这是悦乐书的第283次更新,第300篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第151题(顺位题号是665).给定一个包含n个整数的数组,您的任务是通过修改最多1个元 ...
- 英语口语练习系列-C05-水电
<登幽州台歌>·陈子昂 陈子昂(公元659-公元700年),唐代文学家,初唐诗文革新人物之一. Num 诗句 1 前不见古人, 2 后不见来者. 3 念天地之悠悠, 4 独怆然而涕下! T ...
- linux安装成功后怎么调出终端
一.Ubuntu 桌面如下,点击搜索 二.输入terminal 终端 三.锁定到菜单栏 四.接下来就可以练习linux下的常用命令,如:ls mkdir cat touch 等等 这些命令后 ...