题目链接

BZOJ 3864

题意简述

设字符集为ATCG,给出一个长为\(n(n \le 15)\)的字符串\(A\),问有多少长度为\(m(m \le 1000)\)的字符串\(B\)与\(A\)的最长公共子序列为\(i\),对所有\(0 \le i \le n\)输出答案。

题解

传说中的计算机理论科科科科科学家cls的DP套DP。

因为看别人写的题解我都看不懂……所以我在这篇题解中,换一种方式讲解,从暴力一点点优化得到DP套DP,应该更容易理解。

暴力怎么写呢?显然是枚举所有可能的字符串\(B\),然后对每一个都用经典的DP,求出与\(A\)的LCS。写个伪代码:

dfs(cur)
if(cur > m)
for(i: 1 -> m)
for(j: 1 -> n)
f[i][j] = max(f[i - 1][j], f[i][j - 1])
if(b[i] == a[j]) f[i][j] = max(f[i][j], f[i - 1][j - 1] + 1)
ans[f[m][n]]++
return;
for(c in {A, T, C, G})
b[cur] = c
dfs(cur + 1)

考虑略微更改一下暴力的顺序,从把字符串枚举完再DP求LCS,变成一边枚举一边DP,并把\(f[cur]\)传入到递归函数中。

dfs(cur, f[])
if(cur > m)
ans[f[n]]++
return;
for(c in {A, T, C, G})
for(i : 1 -> n)
newf[i] = max(f[i], newf[i - 1])
if(c == a[i]) newf[i] = max(newf[i], f[i - 1] + 1)
dfs(cur + 1, newf)

往函数里传一个数组显然非常菜,考虑状压这个\(f\)数组。显然,一行f数组的每一位\(f[i]\)要么比\(f[i - 1]\)多1,要么和\(f[i - 1]\)相同。那么用一个长为\(n\)的二进制数状压这个\(f\)数组的差分即可。伪代码(\(cnt1(s)\)表示二进制数\(s\)中1的个数,此时就等于\(f[n]\)):

dfs(cur, s)
if(cur > m)
ans[cnt1(s)]++
return;
for(c in {A, T, C, G})
for(i : 1 -> n)
f[i] = f[i - 1] + (s >> (i - 1) & 1)
for(i : 1 -> n)
newf[i] = max(f[i], newf[i - 1])
if(c == a[i]) newf[i] = max(newf[i], f[i - 1] + 1)
for(i: 1 -> n)
t |= (f[i] - f[i - 1]) << (i - 1)
dfs(cur + 1, t)

\(s\)显然有很多重复的,每层DFS都这样算一遍非常浪费,因为这段代码中\(s\)对应的\(t\)只和\(c\)有关,不如预处理出每个\(s\)在\(B[cur] == c\)时能转移到哪个状态\(t\)(预处理方法就和上面这段代码中的那部分一样)。设这个状态\(t\)为\(trans[s][c]\)。

dfs(cur, s)
if(cur > m)
ans[cnt1(s)]++
return;
for(c in {A, T, C, G})
dfs(cur + 1, trans[s][c])

这个DFS都变成这样了,忍不住考虑能不能把它变成DP。用\(dp[i][s]\)表示字符串\(B\)长为\(i\),对应的数组\(f\)状压后为\(s\)的方案数。

dp[0][0] = 1
for(i : 1 -> m)
for(s: 1 -> (1 << n) - 1)
for(c in {A, T, C, G})
dp[i][trans[s][c]] += dp[i - 1][s]
for(s: 1 -> (1 << n) - 1)
ans[cnt1(s)] += dp[m][s]

至此你就从DFS暴力一步步优化出了这道题的DP套DP解法!

AC代码

#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <iostream>
#define space putchar(' ')
#define enter putchar('\n')
using namespace std;
typedef long long ll;
template <class T>
void read(T &x){
char c;
bool op = 0;
while(c = getchar(), c < '0' || c > '9')
if(c == '-') op = 1;
x = c - '0';
while(c = getchar(), c >= '0' && c <= '9')
x = x * 10 + c - '0';
if(op) x = -x;
}
template <class T>
void write(T x){
if(x < 0) putchar('-'), x = -x;
if(x >= 10) write(x / 10);
putchar('0' + x % 10);
} const int N = 15, M = 1005, P = 1000000007;
int T, n, m, id[128], a[N];
int bcnt[1<<N], trans[1<<N][4], f[2][1<<N];
char str[N]; void init_trans(){
static int pre[N], cur[N];
for(int s = 0; s < (1 << n); s++){
if(s) bcnt[s] = bcnt[s >> 1] + (s & 1);
pre[0] = s & 1;
for(int i = 1; i < n; i++)
pre[i] = pre[i - 1] + (s >> i & 1);
for(int c = 0; c < 4; c++){
int t = 0;
cur[0] = pre[0];
if(c == a[0]) cur[0] = 1;
t |= cur[0];
for(int i = 1; i < n; i++){
cur[i] = max(cur[i - 1], pre[i]);
if(c == a[i]) cur[i] = max(cur[i], pre[i - 1] + 1);
t |= (cur[i] - cur[i - 1]) << i;
}
trans[s][c] = t;
}
}
}
void inc(int &x, int y){
x += y;
if(x >= P) x -= P;
}
void calc_f(){
int pre = 1, cur = 0;
memset(f[1], 0, sizeof(f[1]));
f[1][0] = 1;
for(int i = 0; i < m; i++){
for(int s = 0; s < (1 << n); s++)
f[cur][s] = 0;
for(int s = 0; s < (1 << n); s++)
if(f[pre][s]){
for(int c = 0; c < 4; c++)
inc(f[cur][trans[s][c]], f[pre][s]);
}
swap(pre, cur);
}
static int ans[N + 1];
for(int i = 0; i <= n; i++) ans[i] = 0;
for(int s = 0; s < (1 << n); s++)
inc(ans[bcnt[s]], f[pre][s]);
for(int i = 0; i <= n; i++)
write(ans[i]), enter;
} int main(){ id['A'] = 0, id['T'] = 1, id['C'] = 2, id['G'] = 3;
read(T);
while(T--){
scanf("%s%d", str, &m);
n = strlen(str);
for(int i = 0; i < n; i++)
a[i] = id[int(str[i])];
init_trans();
calc_f();
} return 0;
}

BZOJ 3864 Hero meet devil 超详细超好懂题解的更多相关文章

  1. bzoj 3864: Hero meet devil [dp套dp]

    3864: Hero meet devil 题意: 给你一个只由AGCT组成的字符串S (|S| ≤ 15),对于每个0 ≤ .. ≤ |S|,问 有多少个只由AGCT组成的长度为m(1 ≤ m ≤ ...

  2. bzoj 3864: Hero meet devil

    bzoj3864次元联通们 第一次写dp of dp (:з」∠) 不能再颓废啦 考虑最长匹配序列匹配书转移 由于dp[i][j]的转移可由上一行dp[i-1][j-1],dp[i-1][j],dp[ ...

  3. bzoj 3864: Hero meet devil(dp套dp)

    题面 给你一个只由\(AGCT\)组成的字符串\(S (|S| ≤ 15)\),对于每个\(0 ≤ .. ≤ |S|\),问 有多少个只由\(AGCT\)组成的长度为\(m(1 ≤ m ≤ 1000) ...

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

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

  5. BZOJ 3864 Hero Meets Devil

    题目大意 给定一个由AGCT组成的串\(t\), 求对于所有的\(L \in [1, |t|]\), 有多少个由AGCT组成的串\(s\)满足\(LCS(s, t) = L\). Solution 传 ...

  6. 【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 ...

  7. bzoj千题计划241:bzoj3864: Hero meet devil

    http://www.lydsy.com/JudgeOnline/problem.php?id=3864 题意: 给你一个DNA序列,求有多少个长度为m的DNA序列和给定序列的LCS为0,1,2... ...

  8. HDU 4899 Hero meet devil(状压DP)(2014 Multi-University Training Contest 4)

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

  9. bzoj3864: Hero meet devil

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

随机推荐

  1. LinqPad的变量比较功能

    LinqPad是一个非常方便的C#工具(有免费版和收费版). 今天发现它的变量比较功能真是方便啊.且看3行代码产生如下结果: 说明:图中两个变量的成员属性值分别用红色和绿色背景标注:图很长,只截取了一 ...

  2. Kafka基础系列第1讲:Kafka的诞生背景及应用

    Kafka 是由 LinkedIn 开发的一个分布式的消息系统,使用 Scala 编写,它以可水平扩展和高吞吐率而被广泛使用.目前越来越多的开源分布式处理系统如 Cloudera.Apache Sto ...

  3. [已解决]An unhandled exception occurred while processing the request.

    An unhandled exception occurred while processing the request. InvalidOperationException: The layout ...

  4. flask_admin 笔记七 扩展功能

    高级功能 1,开启CSRF保护 要将CSRF保护添加到由ModelView实例生成的表单中,请通过指定form_base_class参数在ModelView子类中使用SecureForm类: from ...

  5. Nagios图像绘制插件PNP4Nagios部署和测试

    注:本篇博客Nagios版本Nagios-3.5.1 1. 概述2. 关于PNP4Nagios3. 部署PNP4Nagios3.1 下载PNP4Nagios3.2 编译安装3.3 目录文件说明4. 配 ...

  6. 一文让你熟练掌握Linux的ncat(nc)命令

    一文让你熟练掌握Linux的ncat(nc)命令 ncat 或者说 nc 是一款功能类似 cat 的工具,但是是用于网络的.它是一款拥有多种功能的 CLI 工具,可以用来在网络上读.写以及重定向数据. ...

  7. High-level structure of a simple compiler高級結構的簡單編譯器

    1.lexical analysis,which analyzes the character string presented to it and divides it up into tokens ...

  8. Redis常见问题和解决办法梳理

    =============Redis主从复制问题和解决办法 ================= 一.Redis主从复制读写分离问题 1)数据复制的延迟读写分离时,master会异步的将数据复制到sla ...

  9. 一个数据表通过另一个表更新数据(在UPDAT语句中使用FROM子句)

    在sql server中,update可以根据一个表的信息去更新另一个表的信息. 首先看一下语法: update A SET 字段1=B表字段表达式, 字段2=B表字段表达式   from B WHE ...

  10. [转]Spring通过@Value注解注入属性的几种方式

    原文地址:https://blog.csdn.net/csujiangyu/article/details/50945486 ------------------------------------- ...