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) 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.

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

题目大意:从一串长度为L的序列中选出包含给定长度为M的子序列的最长序列(子序列元素的相对位置不能发生改变),输出其长度

思路:1、将给定长度为M的子序列的位置编个号,从左往右编号递增,要保证子序列元素的相对位置不发生改变,也就是说子序列元素的编号从左往右不下降,从而可以转换成最长不下降子序列的问题(动态规划-LIS)

2、转换为最长不下降子序列问题:从长度为L的序列的序列中选出包含给定长度为M的子序列元素的元素,按照原来的顺序存到A数组中,对A数组进行动态规划

3、最长不下降子序列的问题(动态规划-LIS)的状态方程

(j=1,2,…,i-1&&A[j]<A[i])

4注意编号用map可能会超时,可以自己用数组实现哈希编号

AC代码

#include <iostream>
#include <vector>
using namespace std;
int main() {int n, m, k;scanf("%d", &n);scanf("%d", &m);int mp[10000] = {0};for (int i = 1; i <= m; i++) {int temp;scanf("%d", &temp); mp[temp] = i;} scanf("%d", &k);vector<int> A; for (int i = 0; i < k; i++) {int temp;scanf("%d", &temp); if (mp[temp]) {A.push_back(temp);}}vector<int> dp(A.size());int ans = -1;for (int i = 0; i < A.size(); i++) {dp[i] = 1;for (int j = 0; j < i; j++) {if (mp[A[i]] >= mp[A[j]] && dp[j] + 1 > dp[i]) {dp[i] = dp[j] + 1;}}ans =max(ans, dp[i]); }cout << ans;return 0;
}

更多PAT甲级题目:请访问PAT甲级刷题之路--题目索引+知识点分析(正在进行),感谢支持!

1045 Favorite Color Stripe(30分)-PAT甲级相关推荐

  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分)

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

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

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

  5. 【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 ...

  6. 1045. Favorite Color Stripe (30)

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

  7. pat1045. Favorite Color Stripe (30)

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

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

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

  9. L3-011 直捣黄龙 (30 分)-PAT 团体程序设计天梯赛 GPLT

    本题是一部战争大片 -- 你需要从己方大本营出发,一路攻城略地杀到敌方大本营.首先时间就是生命,所以你必须选择合适的路径,以最快的速度占领敌方大本营.当这样的路径不唯一时,要求选择可以沿途解放最多城镇 ...

最新文章

  1. JavaScript基本知识
  2. 单应矩阵(Homography)基本概念和代码测试
  3. 要学DOT NET了
  4. win2003 IIS6配置PHP 5.3.3(fastCGI方式+eAccelerator)+ASP.NET 4.0(MVC3)
  5. JQuery EasyUI入门
  6. 计算机c语言期末答案,大学计算机C语言期末考试试题A.doc
  7. java7 diamond_Java 7 中的新功能
  8. datatable java排序,Java实现DataTable的过滤,排序,聚合功能
  9. Git报错解决:fatal: unable to access ‘https://github.com/...‘: OpenSSL SSL_read:..., errno 10054
  10. 基于SpringBoot+Vue的音乐网站项目-附源码+报告
  11. 报错:OPC读完成报错 索引超出数组界限
  12. 【原创】2021-2001广东统计年鉴面板数据、珠三角数据、广东年鉴(可直接使用)
  13. gtp6 linux 启动_glibc.i686安装
  14. 计算机信息网络安全保护管理条例,中华人民共和国计算机信息系统安全保护条例...
  15. mysql中的不等于_sql中怎么表示“不等于”(数据库,SQL)
  16. CSS3 排版属性盒子模型 第二个模块
  17. YUV YCbCr 区别
  18. SQL注入的一般过程
  19. 网络编程学习——名字与地址转换(一)
  20. 互联网公司无线覆盖解决方案

热门文章

  1. jboss esb_与JBoss ESB和LegStar的大型机集成
  2. 编程挑战1:抽签问题
  3. Android应用程序获取ROOT权限的方法
  4. html 实现 平方展示
  5. Phabricator使用说明
  6. java 商城系统架构之第三篇——集群架构搭建
  7. 小白的 java 修炼之路 (求师问道)
  8. linux chrome浏览器设置字体
  9. 男孩和女孩-爱情需要理解
  10. 【CUDA安装详细教程】