题干:

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

题目大意:

Eva试着从给出的布条中选出她自己的彩色布条。她想要仅仅保留按照她最喜欢的顺序排列的她喜欢的颜色,并且剪掉那些不喜欢的部分再将剩下的部分缝在一起来组成她最喜欢的彩色布条。所以她需要你的帮助来找出最好的结果。
注意结果可能不为1,但是你只需要告诉她最大的长度。举个例子,给你一个 {2 2 4 1 5 5 6 3 1 1 5 6}.的彩色布条。如果Eva最喜欢的顺序排列的最喜欢的颜色为{2 3 1 5 6},这样她有4种可能的最佳方案 {2 2 1 1 1 5 6}, {2 2 1 5 5 5 6}, {2 2 1 5 5 6 6}, 和{2 2 3 1 1 5 6}。(注意  最喜欢的顺序排列  的每个数字都不重复)

解题报告:

因为最喜欢的顺序排列的每个数字都不重复,也就是说我可以给这些数字重新定序。这样就转化为一个LIS问题了。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define FF first
#define SS second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
int n,m,len;
int a[222],b[MAX],dp[MAX],buf[MAX],top;
int bk[222];//颜色出现的最后一个位置
int main()
{memset(bk,-1,sizeof bk);//initcin>>n >> m;for(int i = 1; i<=m; i++) cin>>a[i],bk[a[i]] = i;cin>>len;for(int x,i = 1; i<=len; i++) {scanf("%d",&x);if(bk[x] != -1) {b[++top] = bk[x];}}for(int i = 1; i<=top; i++) {dp[i] = 1;for(int j = 1; j<i; j++) {if(b[i] >= b[j]) dp[i] = max(dp[i],dp[j]+1);}}printf("%d\n",*max_element(dp+1,dp+top+1));return 0 ;
}

总结:

注意对于序列问题,可以用数字大小来表示前后关系。

还有另一种方法:看不太懂

https://blog.csdn.net/tjj1998/article/details/79951718

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#define INF 99999999
using namespace std;
int N,M,L;
int dp[210],v[210];
int main(){scanf("%d%d",&N,&M);int a;for(int i=0;i<M;i++){scanf("%d",&a);if(!v[a])v[a]=i+1;}scanf("%d",&L);for(int i=0;i<L;i++){scanf("%d",&a);if(v[a])for(int j=M;j>=v[a];j--){for(int k=v[a];k>=0;k--)dp[j]=max(dp[j],dp[k]+1);}}printf("%d\n",dp[M]);return 0;
}

【PAT - 甲级1045】Favorite Color Stripe(30分)(dp,LIS类问题)相关推荐

  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甲级1076 Forwards on Weibo (30 分) :[C++题解]图论、bfs

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

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

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

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

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

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

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

  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. aptana对齐快捷键ctrl+shift+f
  2. 【SmartJob】常规统计部署(报表使用的数据)
  3. Nvidia DX10 Lighting例子解析
  4. 申明   csdn的博客wjszf 也是本人的
  5. 洛谷P7515:矩阵游戏(差分约束)
  6. Linux系统编程(五)时序竞态
  7. mysql 安全删除_mysql的binlog安全删除的一种方法
  8. 5 分钟掌握智联招聘网站爬取并保存到 MongoDB 数据库
  9. 如何在windows下把硬盘格式化成EXT3格式?
  10. 基于android下的amr转mp3
  11. 数据分析步骤(思维导图)
  12. 百度BAE专业版申购SSL证书
  13. larval 捕获mysql错误_larval如何捕获mysql错误
  14. illumina测序的一些注意事项
  15. 副业该怎么选择,适合新手的四个副业项目,零基础也可操作的兼职
  16. Visual Studio界面颜色更换 及 Visual Assist X助手使用
  17. 云流量成为数据中心的王者 五年内全球超大规模IDC将不断出现
  18. 自考计算机可以考研的学校,自考成功后想考研,这五个问题你了解清楚了吗?...
  19. 谷歌Chrome浏览器自动翻译导致前端页面数据错乱问题
  20. 航天广电广播系统服务器调试,广播系统应该做哪些调试

热门文章

  1. discuzX 帖子 有的图片没输出 [attach]12323[/attach]的解决办法
  2. [转] UML中关联、依赖、聚集等关系的异同
  3. PAT-Mars number
  4. 将一个5X5的矩阵中最大的元素放在中心, 4个角分别放4个最小的元素(顺序为从左到右,从上到下,从小到大存放)其余数字从小到大
  5. 撤销 恢复快捷键 Linux,Linux Vim撤销和恢复撤销快捷键用法详解
  6. 做企业网站为什么要服务器呢,企业为什么要建站?
  7. 多个html如何套用套一个头部,Vue.js项目中管理每个页面的头部标签的两种方法...
  8. java 打印abcd_用JAVA编程统计字符串ABCD123!@#$%ab中大写字母、小写字母、数字、其它字符的个数并打印出来...
  9. socket 编程入门教程(一)TCP server 端:7、接收与发送
  10. 设计模式C++实现 —— 外观模式、组合模式