题解 Hero meet devil
题目大意
给出一个长度为 \(n\) 的字符串,对于每个 \(k\in [0,n]\),求出有多少个长度为 \(m\) 的字符串满足两者最长公共子序列长度为 \(k\)。
\(n\le 15,m\le 10^3\),答案对 \(10^9+7\) 取模。
思路
难得扣shi。。。
首先,我们发现如果对两个已知字符串求最长公共子序列,那么我们可以设 \(f_{i,j}\) 表示第 1 个字符串匹配到 \(i\),第 2 个字符串匹配到 \(j\) 的最长匹配长度。不难得到转移式:
\]
\]
于是你发现我们一个字符串其实并不需要知道每一位,我们只需要知道 \(f_{i,j}\) 即可。但是你发现你不好把这个压进状态里面,但是你发现对于每一个 \(i\),\(j\) 每变化 \(1\) 位答案最多只会增加 \(1\),于是你可以考虑把每一位的增加量存进状态里面,这样你就可以做到 \(\Theta(2^nm)\)了。
\(\texttt{Code}\)
#include <bits/stdc++.h>
using namespace std;
#define Int register int
#define mod 1000000007
#define MAXM 1005
#define MAXN 15
template <typename T> inline void read (T &t){t = 0;char c = getchar();int f = 1;while (c < '0' || c > '9'){if (c == '-') f = -f;c = getchar();}while (c >= '0' && c <= '9'){t = (t << 3) + (t << 1) + c - '0';c = getchar();} t *= f;}
template <typename T,typename ... Args> inline void read (T &t,Args&... args){read (t);read (args...);}
template <typename T> inline void write (T x){if (x < 0){x = -x;putchar ('-');}if (x > 9) write (x / 10);putchar (x % 10 + '0');}
char s[MAXN];
int n,m,ans[MAXN],dp[1 << MAXN][MAXM];
void Add (int &a,int b){a = a + b >= mod ? a + b - mod : a + b;}
int a[MAXN],b[MAXN],trans[1 << MAXN][4];
int getid (char c){return c == 'A' ? 0 : (c == 'T' ? 1 : (c == 'C' ? 2 : 3));}
void init (){
for (Int S = 0;S < 1 << n;++ S){
for (Int i = 1;i <= n;++ i) a[i] = a[i - 1] + (S >> i - 1 & 1);
for (Int i = 0;i < 4;++ i){
for (Int j = 1;j <= n;++ j)
if (getid (s[j]) == i) b[j] = a[j - 1] + 1;
else b[j] = max (b[j - 1],a[j]);
trans[S][i] = 0;
for (Int j = 1;j <= n;++ j) trans[S][i] |= b[j] - b[j - 1] << j - 1;
}
}
}
int popcount (int S){
int res = 0;for (Int i = 0;i < n;++ i) res += S >> i & 1;
return res;
}
signed main(){
int t;read (t);
while (t --> 0){
scanf ("%s",s + 1),read (m);
n = strlen (s + 1),init ();
memset (dp,0,sizeof (dp)),dp[0][0] = 1;
for (Int i = 0;i < m;++ i)
for (Int S = 0;S < 1 << n;++ S)
for (Int j = 0;j < 4;++ j)
Add (dp[trans[S][j]][i + 1],dp[S][i]);
for (Int i = 0;i <= n;++ i) ans[i] = 0;
for (Int S = 0;S < 1 << n;++ S) Add (ans[popcount (S)],dp[S][m]);
for (Int i = 0;i <= n;++ i) write (ans[i]),putchar ('\n');
}
return 0;
}
题解 Hero meet devil的更多相关文章
- 【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 ≤ ...
- BZOJ 3864 Hero meet devil 超详细超好懂题解
题目链接 BZOJ 3864 题意简述 设字符集为ATCG,给出一个长为\(n(n \le 15)\)的字符串\(A\),问有多少长度为\(m(m \le 1000)\)的字符串\(B\)与\(A\) ...
- BZOJ3864 & HDU4899:Hero meet devil——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=3864 http://acm.hdu.edu.cn/showproblem.php?pid=4899 ...
- bzoj3864: Hero meet devil
Description There is an old country and the king fell in love with a devil. The devil always asks th ...
- 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 ...
- BZOJ3864: Hero meet devil(dp套dp)
Time Limit: 8 Sec Memory Limit: 128 MBSubmit: 397 Solved: 206[Submit][Status][Discuss] Description ...
- 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 3864: Hero meet devil(dp套dp)
题面 给你一个只由\(AGCT\)组成的字符串\(S (|S| ≤ 15)\),对于每个\(0 ≤ .. ≤ |S|\),问 有多少个只由\(AGCT\)组成的长度为\(m(1 ≤ m ≤ 1000) ...
随机推荐
- spring AOP事务
1 <bean id="tttt" class="com.ry.project.dataSouces.UserLogger"/> 2 <aop ...
- Caffe 快速入门笔记
官网:http://caffe.berkeleyvision.org/ 其中包含Notebook Example方便入门学习 只是使用她的库还是比较简单,其难点在于: 安装 源码 训练好的模型,用于迁 ...
- 对于MySQL远程连接中出现的一个问题总结
2021年9月3日更新补充 (真的心累,本来是个小问题,但是网上帖子都基本差不多,基本都是相同的操作,导致搜了半个多小时才解决) 一.首先为什么要重新发一次呢,因为我发现上次写的这个记录是不完善甚至是 ...
- Git使用教程五
基于ssh协议(推荐) 该方式与前面https方式相比,只是影响github对于用户的身份鉴权方式,对于git的具体操作(如提交本地.添加注释.提交远程等操作)没有任何影响. 生成公私钥对指令(需 ...
- 基于Linux系统下Apache服务器的安装部署
企业中常用的web服务,用来提供http://(超文本传输协议). web系统是客户端/服务器模式的,所以应该有服务器和客户端里两个部分.常用的服务器程序时Apache,常用的客户端程序是浏览器.ww ...
- 基于Linux的系统排错
1.系统引导过程概述 2.系统异常及恢复 [1]grub系统引导 1)mbr上446字节丢失 模拟问题: dd if=/dev/zero? of=/dev/vda? bs=446? count=1? ...
- 20210716 noip17
考场 终于有一场在晚上考了 T1 随便画了画就发现要求每个点的后继个数,想起来有 dfs 和 toposort 两种方法,感觉很稳 T2 裸的网络流有 70pts?!真香 一看 T3 就想起了 Mst ...
- eclipse的C/C++开发搭建
环境 宿主机:ubuntu 16.04 交叉编译:gcc-linaro-4.9-2014.11 ubuntu自带源 eclipse 安装(想使用新版直接跳过) 安装eclipse sudo apt-g ...
- SQL Server 使用bcp进行大数据量导出导入
转载:http://www.cnblogs.com/gaizai/archive/2010/04/17/1714389.html SQL Server的导出导入方式有: 在SQL Server中提供了 ...
- aes加解密前后端-后台
一.web.xml: <filter> <filter-name>fastLoginFilter</filter-name> <filter-class> ...