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 ...
随机推荐
- zoj 4099 Extended Twin Composite Number
Do you know the twin prime conjecture? Two primes and are called twin primes if . The twin prime c ...
- Java:JVM垃圾回收(GC)机制
JVM垃圾回收算法 1.标记清除(Mark-Sweep) 原理: 从根集合节点进行扫描,标记出所有的存活对象,最后扫描整个内存空间并清除没有标记的对象(即死亡对象)适用场合: 存活对象较多的情况下比较 ...
- Selenium常用API的使用java语言之14-多窗口切换
在页面操作过程中有时候点击某个链接会弹出新的窗口, 这时就需要主机切换到新打开的窗口上进行操作.WebDriver提供了switchTo().window()方法可以实现在不同的窗口之间切换. 以百度 ...
- 我们什么时候应该在C程序中使用指针?
回答: 传递大型结构喜欢服务器请求或响应数据包. 实现链表和二叉树. 使用GPIO或硬件寄存器. 从函数中获取地址或更新值(通过引用调用) 创建动态数组. 使用函数指针创建回调函数. 注意:除此之外, ...
- idea项目打包和在linux的部署
1.将项目打包 2.先clean,后package 3.将打好的包上传到linux 使用cd指令到要上传的文件的目录 4.输入rz -y 5.解压tar.gz格式: tar -zxvf filenam ...
- 判断大文件是否上传成功(一个大文件上传到ftp,判断是否上传完成)
大文件上传ftp,不知道有没有上传完成,如果没有上传完成另一个程序去下载这个文件,导致下载不完整. 判断一个文件是否上传完成的方法: /** * 间隔一段时间去计算文件的长度来判断文件是否写入完成 * ...
- kalilinux MSF数据库的连接
需要自动连接数据库.如下设置.
- 超过20g的文件+上传
demo下载地址:jsp-Eclipse,jsp-MyEclipse,PHP,ASP.NET 教程:ASP.NET,JSP,PHP 一. 功能性需求与非功能性需求 要求操作便利,一次选择多个文件和文件 ...
- css3 2D变形(transform)移动、缩放、旋转、倾斜
一. translate(x,y) 或者translateX(x)或者translateY(y) 注意点:translateX(x)和translateY(y)大写X和Y:只写一个值,默认y=0. & ...
- [转]引用模板类中定义的类型(用typedef或using)以及auto、decltype、typename的使用
一.背景 使用typedef或者using定义类型别名是非常常见的手段,在c++里面,有时为了封装性,模块性等原因还会在某一个namespace或者class内部定义类型别名. 最近在写c++代码的时 ...