题解 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) ...
随机推荐
- jq的常用事件及其案例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 2021 年 9 月 TIOBE 指数 C# 增长突破 1.2%
TIOBE 编程社区指数是编程语言流行程度的指标.该指数每月更新一次.评级基于全球熟练工程师.课程和第三方供应商的数量.谷歌.必应.雅虎.维基百科.亚马逊.YouTube 和百度等流行搜索引擎用于计算 ...
- 浅谈KMP模式匹配算法
普通的模式匹配算法(BF算法) 子串的定位操作通常称为模式匹配算法 假设有一个需求,需要我们从串"a b a b c a b c a c b a b"中,寻找内容为"a ...
- git 提交本地项目
在新文件夹中] 1.右键 git bash here,执行 2.git init 生成.git文件,存在则跳过 依次执行 1.git add . 2.git commit -m "提交信息& ...
- golang接口类型判断 VS 接口类型查询
接口类型判断:接口.(类型) 1.类型查询:查询接口数据对应的类型是否是指定的类型 2.类型转化:如果是指定类型,就将数据转化为接口类型查询: 1.reflect.TypeOf(接口) 2.接口.(T ...
- 在EXCEL中批量添加超链接
在单元格中输入函数 =HYPERLINK(链接位置,[显示文本])
- go语言游戏服务端开发(三)——服务机制
五邑隐侠,本名关健昌,12年游戏生涯. 本教程以Go语言为例. P2P网络为服务进程间.服务进程与客户端间通信提供了便利,在这个基础上可以搭建服务. 在服务层,通信包可以通过定义协议号来确定该包怎 ...
- JAVA修饰符优先级先后顺序规范
在实际的开发中,会遇到定义静态常量时,有的人使用的修饰符顺序不一致,例如 ... static final ... 或者 ... final static ... 于是找到了下规范,分享下 优先级 修 ...
- 有备无患「GitHub 热点速览 v.21.38」
作者:HelloGitHub-小鱼干 数据库最重要的一个功能是容灾备份,备份不只是对数据库重要,对日常工作生活的我们一样重要,比如花了一个工作日写的代码没有备份(虽然可能只有 1 行-)总归是一个让人 ...
- Consul+Ocelot+Polly在.NetCore中使用(.NET5)-网关Ocelot+Consul
相关文章 Consul+Ocelot+Polly在.NetCore中使用(.NET5)-Consul服务注册,服务发现 Consul+Ocelot+Polly在.NetCore中使用(.NET5)-网 ...