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}.
Each input file contains one test case. For each case, the first line contains a positive integer N (≤200) 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 (≤200) followed by M Eva's favorite color numbers given in her favorite order. Finally the third line starts with a positive integer L (≤104) 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.
For each test case, simply print in a line the maximum length of Eva's favorite stripe.
6
5 2 3 1 5 6
12 2 2 4 1 5 5 6 3 1 1 5 6
7
利用map,重新将给出的数据按照先后顺序重新定义新的数字。例如题目给出的:2 3 1 5 6;这些数字对应的新编号就是:0 1 2 3 4;再将不喜欢的数字去除,这样就可以将后面给出的数字转换成求非递减的最长数列的长度。
主要就是求最长非递减数列的长度,因该如下(dp数组的作用就是记录到下标为i的数字为止的最长非递减数列的长度):
- int r_max = 0;int dp[num];
- for(int i=0;i
- { dp[i]=1;
- for(int j=0;j
- if(color[i]>=color[j]&&dp[j]+1>dp[i]){
- dp[i]=dp[j]+1;
- }
- }
- r_max=max(r_max,dp[i]);
- }
代码:
- #include
- using namespace std;
-
- int main(){
- int N,M,L;
- cin>>N>>M;
- map<int,int> order;//将颜色转换成数字
- for(int i=0;i
- int t;
- cin>>t;
- order[t] = i;
- }
- cin>>L;
- int color[L];
- int num = 0;
- for(int i=0;i
- int t;
- cin>>t;
- if(order.count(t)){//如果是喜欢的颜色则记录下来
- color[num++] = order[t];
- }
- }
- int r_max = 0;int dp[num];
- for(int i=0;i
- { dp[i]=1;
- for(int j=0;j
- if(color[i]>=color[j]&&dp[j]+1>dp[i]){
- dp[i]=dp[j]+1;
- }
- }
- r_max=max(r_max,dp[i]);
- }
- cout<
- return 0;
- }
-
相关阅读:
Springboot+高校考勤小程序 毕业设计-附源码131039
基于springboot+java+vue的健身房课程预约信息网站-计算机毕业设计
流行的Python库numpy及Pandas简要介绍
【MySql密码爆破脚本】用于其他爆破工具无法使用的情况下
NCCL源码解析③:机器内拓扑分析
编程学:关于同类词的等长拼写问题
Java Double parseDouble(String s)方法具有什么功能呢?
阿里云的ACA认证到底是个啥?有用吗?
MySQL数据库基础:JSON函数各类操作一文详解
谷歌悄悄上线新应用,欲用“Switch to Android”吸引苹果用户
-
原文地址:https://blog.csdn.net/weixin_55202895/article/details/126471831