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;
- }
-
相关阅读:
【DNS系列-K8S排错】CoreDNS 新增 host 解析不生效
JavaScript中DOM文档事件
2023五一杯数学建模竞赛ABC题思路解析+代码+论文
可自由搭建的能源管理平台,轻松实现高效节能
Gitter+Sidecar制作聊天室
【python接口测试】requests库安装和导入
来自BAT的一份Java高级开发岗面试指南
react native使用3-基础环境搭建1
TMS FMX Cloud提供集成元素
Http CORS 跨域请求
-
原文地址:https://blog.csdn.net/weixin_55202895/article/details/126471831