PAT 甲级 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 favorite colors in her favorite order by cutting off those unwanted pieces and sewing the remaining parts together to form her favorite color stripe.
It is said that a normal human eye can distinguish about less than 200 different colors, so Eva's favorite colors are limited. However the original stripe could be very long, and Eva would like to have the remaining favorite stripe with the maximum length. So she needs your help to find her the best result.
Note that the solution might not be unique, but you only have to tell her the maximum length. For example, given a stripe of colors {2 2 4 1 5 5 6 3 1 1 5 6}. If Eva's favorite colors are given in her favorite order as {2 3 1 5 6}, then she has 4 possible best solutions {2 2 1 1 1 5 6}, {2 2 1 5 5 5 6}, {2 2 1 5 5 6 6}, and {2 2 3 1 1 5 6}.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (≤) which is the total number of colors involved (and hence the colors are numbered from 1 to N). Then the next line starts with a positive integer M (≤) followed by M Eva's favorite color numbers given in her favorite order. Finally the third line starts with a positive integer L (≤) which is the length of the given stripe, followed by L colors on the stripe. All the numbers in a line a separated by a space.
Output Specification:
For each test case, simply print in a line the maximum length of Eva's favorite stripe.
Sample Input:
6
5 2 3 1 5 6
12 2 2 4 1 5 5 6 3 1 1 5 6
Sample Output:
7
题意:
给出m中颜色作为喜欢的颜色(同时也给出顺序),然后给出一串长度为L的颜色序列,现在要去掉这个序列中的不喜欢的颜色,然后求剩下序列的一个子序列,使得这个子序列表示的颜色顺序符合自己喜欢的颜色的顺序,不一定要所有喜欢的颜色都出现。
思路:
在线实现,没输入了一个颜色,如果是属于喜欢的颜色的话,就遍历这种喜欢的颜色之前的所有喜欢的颜色,如喜欢的颜色为2 3 1 5 6,已经输入的颜色序列是2 2 4 1 5 5 ,接下来输入了6,则6在喜欢的颜色里是最后一个,遍历前面的四个颜色2,3,1,5和它6自己,用mx计算这个6添在哪一个数字序列后面可使长度最长,更新dp[x]。
AC代码:
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<string>
#include<cstring>
using namespace std;
int n,m,l;
int fav[];//喜欢的颜色序列
int x;
int dp[];//dp[x]表示以x结尾的 符合条件的最长长度
int v[];//v[x]表示 这个x在喜欢的颜色序列中是第几个,不在序列中则为0
int main()
{
cin>>n;
memset(,sizeof(v),);
memset(,sizeof(dp),);
cin>>m;
for(int i=;i<=m;i++){
cin>>fav[i];
v[fav[i]]=i;//fav[i]在序列中是第i个
}
cin>>l;
int maxl=;
for(int i=;i<=l;i++){
cin>>x;
int mx=;
for(int j=;j<=v[x];j++){//遍历喜欢的颜色的前几个颜色
mx=max(mx,dp[fav[j]]+);//挑出以x结尾的符合条件的最长长度=max(,前某种颜色的最长长度+1)
}
dp[x]=mx;//更新 就算新的mx比dp[x]小,也要更新为mx
maxl=max(dp[x],maxl);//更新总的最长长度
}
cout<<maxl<<endl;
return ;
}
PAT 甲级 1045 Favorite Color Stripe (30 分)(思维dp,最长有序子序列)的更多相关文章
- 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甲级】1045 Favorite Color Stripe (30 分)(DP)
		
题意: 输入一个正整数N(<=200),代表颜色总数,接下来输入一个正整数M(<=200),代表喜爱的颜色数量,接着输入M个正整数表示喜爱颜色的编号(同一颜色不会出现两次),接下来输入一个 ...
 - 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)(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 ...
 - 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 h ...
 
随机推荐
- eclipse cdt 代码对齐
			
ctrl + shift + F 快速对齐 平时对小节太不重视了 .
 - 在使用rem布局的时候,遇到的样式排版混乱问题,
			
在使用rem布局的时候,遇到的样式排版混乱问题, 问题1:设置字体为rem单位,但是没有设置line-height为100%, 即 * { line-height: 1; ...
 - js图片上传 的方法
			
先规划出框架 <div id="AQA" style="width:300px; height:200px; background-color:aquamarine ...
 - SpringMVC拦截器及多拦截器时的执行顺序
			
本文链接:https://blog.csdn.net/itcats_cn/article/details/80371639拦截器的配置步骤 springmvc.xml中配置多个拦截器配置自定义拦截器并 ...
 - Neo4j  在Linux下的安装登录
			
第一步:安装JDK https://blog.csdn.net/qq_33951308/article/details/82933535 第二步:下载并安装neo4j 下载地址 或者直接用wget ...
 - ooo's vimrc
			
colorscheme evening set fillchars=vert:\ ,stl:\ ,stlnc:\ set nu set tabstop=4 set shiftwidth=4 set s ...
 - 爬虫(十八):scrapy分布式部署
			
scrapy部署神器-scrapyd -->GitHub地址 -->官方文档 一:安装scrapyd 安装:pip3 install scrapyd 这里我在另外一台ubuntu lin ...
 - python中如何给散点图上面的特定点做标记
			
今天想在散点图的某些特定的点外面画圆圈标记,从下面的文章找到一些灵感,只要在原来的散点图上面给指点添加相应的标志,设置其透明度就可以实现该想法. 顺便复习下散点图的用法. 大家平时为了直观地显示数据的 ...
 - hbuilder连接模拟器进行联调(逍遥模拟器,MuMu模拟器,夜神模拟器)
			
MuMu模拟器:7555 逍遥模拟器:21503 夜神模拟器:62001 1. 2. 3. 如果上诉方法不好使,可以重启模拟器以及hbuilder,有时可能连接中断,可以重新连接.
 - docker笔记--docker 各系统安装
			
在线安装 Docker 在 CentOS/RHEL 中安装 Docker 在终端中运行下面的命令安装 Docker. sudo yum install -y yum-utils sudo yum-co ...