【PAT甲级】1045 Favorite Color Stripe (30 分)(DP)
题意:
输入一个正整数N(<=200),代表颜色总数,接下来输入一个正整数M(<=200),代表喜爱的颜色数量,接着输入M个正整数表示喜爱颜色的编号(同一颜色不会出现两次),接下来输入一个正整数L(<=10000),代表条带的长度,接着输入L个正整数表示条带上的颜色的编号。输出以喜爱颜色顺序排列的最长子串长度(不必每种颜色都有,只保证相对位置相同,同种颜色可连续)。
AAAAAccepted code:
#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
int a[],pos[];
int dp[];
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n,m;
cin>>n>>m;
int color;
for(int i=;i<=m;++i){
cin>>color;
pos[color]=i;//记录颜色的相对位置
}
int cnt=;
int stripe;
int l;
cin>>l;
for(int i=;i<=l;++i){
cin>>stripe;
if(pos[stripe])
a[++cnt]=pos[stripe],dp[cnt]=;//记录条带上颜色在喜爱条带上的位置
}
int mx=;
for(int i=;i<=cnt;++i){
for(int j=;j<i;++j)//以相对位置j结尾的都可以在后面加上以相对位置i结尾的颜色
if(a[j]<=a[i])
dp[i]=max(dp[i],dp[j]+);//更新以相对位置i结尾的子串的长度
mx=max(mx,dp[i]);//选取最长长度作为答案
}
cout<<mx;
return ;
}
【PAT甲级】1045 Favorite Color Stripe (30 分)(DP)的更多相关文章
- PAT 甲级 1045 Favorite Color Stripe (30 分)(思维dp,最长有序子序列)
1045 Favorite Color Stripe (30 分) Eva is trying to make her own color stripe out of a given one. S ...
- PAT甲级1045. Favorite Color Stripe
PAT甲级1045. Favorite Color Stripe 题意: 伊娃正在试图让自己的颜色条纹从一个给定的.她希望通过剪掉那些不必要的部分,将其余的部分缝合在一起,形成她最喜欢的颜色条纹,以保 ...
- pat 甲级 1045 ( Favorite Color Stripe ) (动态规划 )
1045 Favorite Color Stripe (30 分) Eva is trying to make her own color stripe out of a given one. She ...
- PAT 甲级 1045 Favorite Color Stripe
https://pintia.cn/problem-sets/994805342720868352/problems/994805437411475456 Eva is trying to make ...
- PAT 甲级 1045 Favorite Color Stripe(DP)
题目链接 Favorite Color Stripe 题意:给定$A$序列和$B$序列,你需要在$B$序列中找出任意一个最长的子序列,使得这个子序列也是$A$的子序列 (这个子序列的相邻元素可以重复) ...
- 1045 Favorite Color Stripe (30分)(简单dp)
Eva is trying to make her own color stripe out of a given one. She would like to keep only her favor ...
- PAT 甲级 1068 Find More Coins (30 分) (dp,01背包问题记录最佳选择方案)***
1068 Find More Coins (30 分) Eva loves to collect coins from all over the universe, including some ...
- 1045 Favorite Color Stripe (30)(30 分)
Eva is trying to make her own color stripe out of a given one. She would like to keep only her favor ...
- 1045. Favorite Color Stripe (30) -LCS允许元素重复
题目如下: Eva is trying to make her own color stripe out of a given one. She would like to keep only her ...
随机推荐
- pudn免费下载账号 codeforge积分账号 pudn共享账号 codeforge下载账号
www.pudn.com和www.codeforge.cn网站下载代码很好,没有积分怎么办?那么多好的matlab代码,matlab程序,C,JAVA等等,都要充值啊!!! 下面的账号积分都用完了,大 ...
- bootstrap中响应式表格失灵
当宽度小于768px,由于表格的内容不能填充到出现横向滚动条 单元格内容不够: 出现横向滚动条
- C++泛型算法总结
1 accumulate(b,e,T) 累和(基础和为T) 注意T的类型必须和序列中元素类型相同,如double序列后面的T就必须是0.0,如果是0就会把序列中的数当成int进行求和 2 count( ...
- centos7安装Nginx 配置及反向代理
Nginx是俄罗斯人编写的十分轻量级的HTTP服务器,Nginx,它的发音为“engine X”,是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/POP3/SMTP 代理服务器.Ngin ...
- AcWing 859. Kruskal算法求最小生成树 稠密图
//稠密图 #include <cstring> #include <iostream> #include <algorithm> using namespace ...
- expect 脚本
实现远程执行 /home/dataexa/test/proxy.expect touch proxy.expect #!/usr/bin/expect set timeout 30 spawn ssh ...
- Linux shell sed 命令详解
详细的sed命令详解,请参考https://my.oschina.net/u/3908182/blog/1921761 sed命令常见用途 查找关键词做全局替换 查找某行的关键词做替换 查找关键字所在 ...
- CentOS7修改主机名的三种方法
在CentOS7中,有三种定义的主机名: 静态的(Static hostname) “静态”主机名也称为内核主机名,是系统在启动时从/etc/hostname自动初始化的主机名. 瞬态的(Tansie ...
- XFire客户端调用CXF服务端(四)
前面章节:http://www.cnblogs.com/xiehongwei/p/8082337.html 已经开发出了CXF服务端,现在用XFire开发客户端调用CXF服务端,代码如下: impor ...
- select2多选框初始化默认值和获得值
select2多选自带手动输入搜索功能,可怜我还查寻半天api 获得值: //chang函数获取选择的option $(".js-example").change(function ...