PAT甲级1045 Favorite Color Stripe (30 分):[C++题解]最佳彩色带、DP、公共子序列变形
文章目录
- 题目分析
- 题目链接
题目分析
来源:acwing
分析:这是一个公共子序列的问题。但是有点变式,序列a和序列b不是完全等价的,序列a的每个元素可以对应多个相同元素,而且有些元素可以不使用。比如下例样例,a ={2 3 1 5 6},b ={2 2 4 1 5 5 6 3 1 1 5 6}
a和b的公共子序列可以是:
a取: 2 1 5 6
b取: 2 2 1 1 1 5 6
a只取了一部分,b也取了一部分,但是我们可以看到:a中的一个数可以对应到b中的几个数。
按照最长公共子序列的方式来分析
状态表示:
f[i][j]f[i][j]f[i][j]表示分别在a[1]a[1]a[1]~ a[i]a[i]a[i]和b[1]b[1]b[1] ~b[j]b[j]b[j]中取的最长公共子序列的长度。
状态计算:
请注意:当a[i]==b[j]a[i] == b[j]a[i]==b[j]时,转移不是f[i−1][j−1]+1f[i-1][j-1]+1f[i−1][j−1]+1,而是f[i][j−1]+1f[i][j-1]+1f[i][j−1]+1,这是因为序列a中的数a[i]a[i]a[i]可以对应多个b序列中的数!!
请注意:根据f[i][j]f[i][j]f[i][j]的定义,上图中蓝色情况的计算公式f[i−1][j−1]f[i-1][j-1]f[i−1][j−1]可以包含在f[i−1][j]f[i-1][j]f[i−1][j]或f[i][j−1]f[i][j-1]f[i][j−1]里面,所以能用到的公式就是上面三个红色公式。当然写上也不错,只不过有冗余计算。
多出冗余计算的版本
#include<bits/stdc++.h>
using namespace std;
const int N =210, M =10010;int n ,m , l;
int a[N], b[M];
int f[N][M];int main(){cin >> n;cin >> m;for(int i =1; i<= m; i++) cin>> a[i];cin >> l;for(int i = 1;i <= l; i++) cin >> b[i];for(int i=1; i<= m; i++){for(int j = 1; j<= l; j++){f[i][j] = max(f[i-1][j-1],f[i-1][j]);f[i][j] = max(f[i][j],f[i][j-1]);if(a[i] == b[j])f[i][j] = max(f[i][j] , f[i][j-1]+1);}}cout << f[m][l]<<endl;
}
或者少些冗余计算的ac代码
#include<bits/stdc++.h>
using namespace std;
const int N =210, M =10010;int n ,m , l;
int a[N], b[M];
int f[N][M];int main(){cin >> n;cin >> m;for(int i =1; i<= m; i++) cin>> a[i];cin >> l;for(int i = 1;i <= l; i++) cin >> b[i];for(int i=1; i<= m; i++){for(int j = 1; j<= l; j++){f[i][j] = max(f[i][j-1],f[i-1][j]);if(a[i] == b[j])f[i][j] = max(f[i][j] , f[i][j-1]+1);}}cout << f[m][l]<<endl;
}
题目链接
PAT甲级1045 Favorite Color Stripe (30 分)
https://www.acwing.com/problem/content/1531/
PAT甲级1045 Favorite Color Stripe (30 分):[C++题解]最佳彩色带、DP、公共子序列变形相关推荐
- PAT甲级-1045 Favorite Color Stripe (30分)
点击链接PAT甲级-AC全解汇总 题目: Eva is trying to make her own color stripe out of a given one. She would like t ...
- 1045 Favorite Color Stripe (30 分)【难度: 中 / 知识点: DP】
https://pintia.cn/problem-sets/994805342720868352/problems/994805437411475456 这一道题,实际上就是一道最长上升子序列的一个 ...
- 1045 Favorite Color Stripe (30分)
题目 Eva is trying to make her own color stripe out of a given one. She would like to keep only her fa ...
- PAT甲级1076 Forwards on Weibo (30 分) :[C++题解]图论、bfs
文章目录 题目分析 题目来源 题目分析 来源:acwing 分析: BFS如何搜前k层?统计前k层的点数. ac代码 #include<bits/stdc++.h> using names ...
- PAT甲级1068 Find More Coins (30 分):[C++题解]DP、背包问题、dp输出方案
文章目录 题目分析 题目链接 题目分析 来源:acwing 分析:m是背包容量,a1,a2,....,ana_1,a_2,....,a_na1,a2,....,an是n个物品,第i个物品的体积是 ...
- PAT甲级1119 Pre- and Post-order Traversals (30分):[C++题解]暴搜dfs、前序遍历和后序遍历求中序遍历
文章目录 题目分析 题目链接 题目分析 分析 给了前序遍历和后序遍历,能够确定根结点,但是左子树和右子树的长度是不确定的.这里采用的解决方案是枚举左子树的结点个数,其实右子树的结点个数也确定了.对于每 ...
- PAT甲级1143 Lowest Common Ancestor (30 分):[C++题解]LCA、最低公共祖先
文章目录 题目分析 题目链接 题目分析 来源:acwing 分析:二叉搜索树的中序遍历是隐含给定的,它的中序遍历就是从小到大排列. 所以这道题先是根据给定的前序遍历和中序遍历,建树. 建树的时候需要用 ...
- 【PAT - 甲级1045】Favorite Color Stripe(30分)(dp,LIS类问题)
题干: Eva is trying to make her own color stripe out of a given one. She would like to keep only her f ...
- 1045. Favorite Color Stripe (30)
题目连接:https://www.patest.cn/contests/pat-a-practise/1045原题如下: Eva is trying to make her own color str ...
最新文章
- python 类变量、实例变量、参数、实例方法、类方法、静态方法 的用法和区别
- 简要说明建设城市大脑三条关键标准规范
- Map-Reduce编程模型gif图片解释
- iOS Ruby出现问题,导致无法安装Pod
- dotnet core入门
- netcore一键部署到linux服务器以服务方式后台运行
- 前端开发 填充padding 0229
- eclipse-在编译项目时js特别慢的问题
- java 拦截html请求参数值_javaweb项目,html文件放在了WebRoot下,如何拦截访问html的请求呀?...
- [译] ASP.NET 生命周期 – ASP.NET 应用生命周期(一)
- 【ELAMN预测】基于遗传算法优化ELMAN神经网络实现电力符合数据回归预测附matlab代码
- snap-社交网络分析
- 2023年深圳市绿色低碳产业扶持计划申报指南
- day03 爬取京东信息,bs4
- 逆向序列号生成算法(三)
- 智能指针手表_反对智能手表
- 在Quartus中调用Modelesim时仿真出现“Error:Failed to find design unit work“可能的解决方法
- 用C语言解一元二次方程
- [BZOJ3161]孤舟蓑笠翁
- python 爬取糗百
热门文章
- 数据库mongodb和mysql对比
- JSONP跨域的原理解析及其实现介绍
- Bluetooth LE(低功耗蓝牙) - 第五部分
- Silverlight Curve Animation / 曲线动画
- wap站点优于传统站点的异同分析
- 如何画透明位图(转)
- 计算氦原子的基态能级
- iis php5.3 mysql_Win2008 R2配置IIS7.5+PHP Manager+PHP5.3+Mysql5.5+Wincache
- python 网关控制家居_在树莓派上搭建智能家居网关
- 【控制】多智能体系统总结。4.控制协议。