题目

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 ( ≤ 200 ) N(\le200) N(≤200) which is the total number of colors involved (and hence the colors are numbered from 1 1 1 to N N N). Then the next line starts with a positive integer M ( ≤ 200 ) M(\le200) M(≤200) followed by M M M Eva’s favorite color numbers given in her favorite order. Finally the third line starts with a positive integer L ( ≤ 1 0 4 ) L(\le10^4) L(≤104) which is the length of the given stripe, followed by L L 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的颜色序列,现在要去掉这个序列中的不喜欢的颜色,然后求剩下序列的一个子序列,使得这个子序列表示的颜色顺序符合自己喜欢的颜色的顺序,不一定要所有喜欢的颜色都出现。

思路

题目只要求输出最长的子序列长度,因此我们可以把输入序列做如下处理:

  1. 当前颜色不是喜欢的颜色,直接去掉;
  2. 当前颜色是喜欢的序列,按已给出的喜欢的颜色顺序编号,把编号存入(把颜色序列转换位编号序列);
    那么这样想的话,这个题就转换为求最长子序列问题,这个最长子序列的要求就是不递减序列,这里我们采用动态规划方法求解;

代码

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;int fav[201];
int ls[10000], dp[10000];int main(){int n, m, l;fill(fav, fav+201, 0);scanf("%d %d", &n, &m);    // 个人觉得n是个多余的数据,没用上哈哈int cnt = 1;for(int i=0; i<m; i++){int t;scanf("%d", &t);fav[t] = cnt++;}cnt = 0;scanf("%d", &l);for(int i=0; i<l; i++){int t;scanf("%d", &t);if(fav[t] > 0)ls[cnt++] = fav[t];    // 存储喜欢颜色的顺序}// 动规求解// 记录动规得出的最大值int ans = 0;for(int i=0; i<cnt; i++){dp[i] = 1;for(int j=0; j<i; j++){if(ls[i] >= ls[j])dp[i] = max(dp[i], dp[j]+1);}ans = max(ans, dp[i]);}printf("%d\n", ans);return 0;
}

1045 Favorite Color Stripe (30分)相关推荐

  1. PAT甲级1045 Favorite Color Stripe (30 分):[C++题解]最佳彩色带、DP、公共子序列变形

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析:这是一个公共子序列的问题.但是有点变式,序列a和序列b不是完全等价的,序列a的每个元素可以对应多个相同元素,而且有些元素可以不使用.比 ...

  2. PAT甲级-1045 Favorite Color Stripe (30分)

    点击链接PAT甲级-AC全解汇总 题目: Eva is trying to make her own color stripe out of a given one. She would like t ...

  3. 1045 Favorite Color Stripe (30 分)【难度: 中 / 知识点: DP】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805437411475456 这一道题,实际上就是一道最长上升子序列的一个 ...

  4. 【PAT - 甲级1045】Favorite Color Stripe(30分)(dp,LIS类问题)

    题干: Eva is trying to make her own color stripe out of a given one. She would like to keep only her f ...

  5. 1045. Favorite Color Stripe (30)

    题目连接:https://www.patest.cn/contests/pat-a-practise/1045原题如下: Eva is trying to make her own color str ...

  6. pat1045. Favorite Color Stripe (30)

    1045. Favorite Color Stripe (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  7. 1045 Favorite Color Stripe(最长不下降子序列)

    1045 Favorite Color Stripe(最长不下降子序列) 题意:按照题目给出的颜色序列找出原颜色序列中对应的子序列,给出的颜色序列不需要在子序列中全部出现. 解法:根据给出的序列对相应 ...

  8. 1045 Favorite Color Stripe(30分)-PAT甲级

    Eva is trying to make her own color stripe(条纹) out of a given one. She would like to keep only her f ...

  9. 1045 Favorite Color Stripe(LIS解法)

    解题思路 本题属于Longest Increasing Sequence最长不下降子序列,但是要注意,LIS当中不会有无效的元素,而本题是有的,所以先要把无效元素过滤掉,才能转化成为LIS问题. 这里 ...

最新文章

  1. 每日一皮:为了找个程序员租客,我拼了!
  2. c语言发送结构体 文件
  3. JAVA学习之常用集合List,Set,Map
  4. C/C++报错:全局变量重定义或是多次定义
  5. 原码一位乘法器设计_十分钟带你彻底搞懂原码、反码、补码
  6. xy轴坐标图数字表示_【相机标定】四个坐标系之间的变换关系
  7. 如何快速REPAIR TABLE
  8. 3.4 SE55表维护生成器
  9. TiDB-新一代数据库入门介绍
  10. Leetcode 513 javascript
  11. 协方差 方差 以及线性相关理解
  12. android-Vibrator的使用
  13. Javaweb实现简易的留言板项目
  14. 【IAR】 This device has been locked for debugging
  15. python:tushare pro 股票每日行情
  16. 20189319《网络攻防》第二周作业
  17. Spring Batch 中的 chunk
  18. C 语言课程设计 最终答辩版 学生通讯录管理系统
  19. 【JZOJ 省选模拟】多项式(poly)
  20. 【转】【Books】程序员必读的30本书籍

热门文章

  1. 混凝土结构耐久性(RCM,氯离子浓度,碳化深度)结构(梁、柱、办、墙、结点)数据库
  2. 加强学习 Linux
  3. Evaluate函数运行时错误438
  4. 实验室可以训练(De)CLIP了!商汤ICLR2022 DeCLIP正式开源!
  5. java中1%4是多少_java语言程序设计基础篇课后编程练习答案2~4章
  6. vs++2010学习版的注册密钥
  7. 单身税时代将至,教你用Python找一个女朋友!(附步骤)
  8. oracle 年份值需介于,ORA-01841: (完整) 年份值必须介于 -4713 和 +9999 之间, 且不为 0情况解决...
  9. 12期 8月期刊自荐
  10. 宝宝患有眼球震颤有哪些危害?严重时可造成斜视和弱视!