点击链接PAT甲级-AC全解汇总

题目:
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 (≤10​4​​ ) 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

题意:
按照给定顺序,从一串数种找出与该顺序相符合的子序列最大长度。

没有学过算法,这种题刚上来有点蒙,看了各路大神的dp思路,还是觉得柳神写得最清楚。借鉴了一下思路。学海无涯。

思路:
先把数串过滤不在排名中的数字,剩下的最次长度也是1;
从前往后对于对于每个数而言,到此为止的最大长度(是以该节点为终点,不然没法考虑到排名为51111的情况),分两种情况

我的代码:

#include<bits/stdc++.h>
using namespace std;int main(){int N,M,L;cin>>N>>M;int rank[N+1]={0};for(int i=1;i<=M;i++){int num;cin>>num;rank[num]=i;//rank[2]=1 数字2的排名是1}cin>>L;vector<int>nums;for(int i=0;i<L;i++){int num;cin>>num;if(rank[num])nums.push_back(num);//没有排名的数就不考虑了}int max_i[nums.size()]={0},max_res=0;for(int i=0;i<nums.size();i++){max_i[i]=1;for(int j=0;j<i;j++)//向前找最大的if(rank[nums[j]]<=rank[nums[i]])max_i[i]=max(max_i[i],max_i[j]+1);max_res=max(max_i[i],max_res);}cout<<max_res<<endl;return 0;
}

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

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

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

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

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

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

  4. PAT甲级1076 Forwards on Weibo (30 分) :[C++题解]图论、bfs

    文章目录 题目分析 题目来源 题目分析 来源:acwing 分析: BFS如何搜前k层?统计前k层的点数. ac代码 #include<bits/stdc++.h> using names ...

  5. PAT甲级1068 Find More Coins (30 分):[C++题解]DP、背包问题、dp输出方案

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析:m是背包容量,a1,a2,....,ana_1,a_2,....,a_na1​,a2​,....,an​是n个物品,第i个物品的体积是 ...

  6. PAT甲级1119 Pre- and Post-order Traversals (30分):[C++题解]暴搜dfs、前序遍历和后序遍历求中序遍历

    文章目录 题目分析 题目链接 题目分析 分析 给了前序遍历和后序遍历,能够确定根结点,但是左子树和右子树的长度是不确定的.这里采用的解决方案是枚举左子树的结点个数,其实右子树的结点个数也确定了.对于每 ...

  7. PAT甲级1143 Lowest Common Ancestor (30 分):[C++题解]LCA、最低公共祖先

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析:二叉搜索树的中序遍历是隐含给定的,它的中序遍历就是从小到大排列. 所以这道题先是根据给定的前序遍历和中序遍历,建树. 建树的时候需要用 ...

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

  9. 1045. Favorite Color Stripe (30)

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

最新文章

  1. 路由协议重分发之RIP协议和EIGEP协议
  2. select2 ajax demo,Select2下拉框示例
  3. python之lxml处理xml
  4. why do you need that a awesome linkedin profile
  5. 定义范围中的备选方案生成、横向思维、创建WBS、工作包定义、WBS、确认范围过程和实施质量过程的关系、联合应用设计和质量功能展开QFD
  6. c语言多线程多个传入参数,关于ssl多线程参数的传递
  7. POJ - 1696 Space Ant(极角排序)
  8. C++中对于类来说头文件(.h)和源文件(.cpp)都应该写些什么 (类的常规创建)
  9. 探索Java日志的奥秘:底层日志系统-log4j2
  10. Linux内核参数(如kernel.shmmax)及Oracle相关参数调整(如SGA_MAX_SIZE)
  11. 李洪强iOS开发Swift篇—02_变量和常量
  12. php嘲讽,PHP为何能够受到大家追捧,又为什么饱受嘲讽?
  13. Ubuntu 编译安装ffmpeg,mplayer,x264全教程支持VDPAU(高清硬解)
  14. 求助:使用foreach函数获取到后台数据时未在表格上渲染的问题
  15. day2--numpy
  16. Android广播时间——实现强制下线功能
  17. 失焦事件触发_JavaScript event 事件详解
  18. 验证码~正则表达式【整理汇总】
  19. 翻译: 深入神经网络概览Dive into Deep Learning
  20. 腾讯会议共享屏幕时,设置演讲者模式仅自己可见

热门文章

  1. lua——userdata使用
  2. Statement常用的方法回顾
  3. COMMONSENSEQA: A Question Answering Challenge Targeting Commonsense Knowledge
  4. Mybatis源码之Statement
  5. Web Components-LitElement 实践
  6. 软件测试(概念Ⅰ) · 软件测试的基本概念 · 什么是需求 · 测试用例的概念 · 软件错误(bug)的概念
  7. java使用jdbc连接oracle,实现自定义sql查询,加载字段注释
  8. CUDA Samples
  9. python 不等于None 不等于空_高考励志标语条幅押韵:不拼不搏等于白活
  10. linux重启网络报错Unit network-manager.service not found.