PAT 甲级 1045 Favorite Color Stripe
https://pintia.cn/problem-sets/994805342720868352/problems/994805437411475456
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 (≤) 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 (≤) followed by M Eva's favorite color numbers given in her favorite order. Finally the third line starts with a positive integer L (≤) 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
代码:
#include <bits/stdc++.h>
using namespace std; int N, M, K;
int dp[10010], num[10010], pos[10010];
int cnt = 0; int main() {
int x;
scanf("%d%d", &N, &M);
for(int i = 1; i <= M; i ++) {
scanf("%d", &x);
pos[x] = i;
} scanf("%d", &K);
for(int i = 0; i < K; i ++) {
scanf("%d", &x);
if(pos[x] >= 1) num[cnt ++] = pos[x];
} int maxx = 0;
for(int i = 0; i < cnt; i ++) {
dp[i] = 1;
for(int j = 0; j < i; j ++) {
if(num[i] >= num[j])
dp[i] = max(dp[i], dp[j] + 1);
}
maxx = max(maxx, dp[i]);
}
printf("%d\n", maxx);
return 0;
}
dp 求最长上升子序列 用他们的位置重组一个数组就阔以了
PAT 甲级 1045 Favorite Color Stripe的更多相关文章
- PAT甲级1045. Favorite Color Stripe
PAT甲级1045. Favorite Color Stripe 题意: 伊娃正在试图让自己的颜色条纹从一个给定的.她希望通过剪掉那些不必要的部分,将其余的部分缝合在一起,形成她最喜欢的颜色条纹,以保 ...
- PAT 甲级 1045 Favorite Color Stripe (30 分)(思维dp,最长有序子序列)
1045 Favorite Color Stripe (30 分) Eva is trying to make her own color stripe out of a given one. S ...
- pat 甲级 1045 ( Favorite Color Stripe ) (动态规划 )
1045 Favorite Color Stripe (30 分) Eva is trying to make her own color stripe out of a given one. She ...
- PAT 甲级 1045 Favorite Color Stripe(DP)
题目链接 Favorite Color Stripe 题意:给定$A$序列和$B$序列,你需要在$B$序列中找出任意一个最长的子序列,使得这个子序列也是$A$的子序列 (这个子序列的相邻元素可以重复) ...
- PAT甲级——A1045 Favorite Color Stripe
Eva is trying to make her own color stripe out of a given one. She would like to keep only her favor ...
- PAT 1045 Favorite Color Stripe[dp][难]
1045 Favorite Color Stripe (30)(30 分) Eva is trying to make her own color stripe out of a given one. ...
- 1045 Favorite Color Stripe 动态规划
1045 Favorite Color Stripe 1045. Favorite Color Stripe (30)Eva is trying to make her own color strip ...
- 1045. Favorite Color Stripe (30) -LCS允许元素重复
题目如下: Eva is trying to make her own color stripe out of a given one. She would like to keep only her ...
- 1045. Favorite Color Stripe (30) -LCS同意元素反复
题目例如以下: Eva is trying to make her own color stripe out of a given one. She would like to keep only h ...
随机推荐
- Django之ORM查询复习与cookie
ORM查询总结: models.Book.objects.filter(**kwargs): querySet [obj1,obj2] models.Book.objects.filter(**kwa ...
- PHP实现DES加密,解决乱码
PHP要实现DES加密,大体上有2个思路.一是用扩展,一是原生PHP实现. 这里推荐一个PHP原生实现的加密,是thinkphp的一个类库: https://github.com/top-think/ ...
- jQuery-iframe加载完成后触发的事件监听
每天学习一点点 编程PDF电子书.视频教程免费下载:http://www.shitanlife.com/code 最近遇到一个问题,要写一个后台管理系统的layout,里面content是一个ifra ...
- 2080 特殊的质数肋骨 USACO (深度优先搜索)
农民约翰的母牛总是产生最好的肋骨. 你能通过农民约翰和美国农业部标记在每根肋骨上的数字认出它们. 农民约翰确定他卖给买方的是真正的质数肋骨,是因为从右边开始切下肋骨,每次还剩下的肋骨上的数字都组成一个 ...
- 第一行代码 3-5 软件也要拼脸蛋-UI界面-更强大的滚动条- 聊天室
动画 https://www.jianshu.com/p/4fc6164e4709 前面说过,RecyclerView可以设置列表中Item删除和添加的动画,在v7包中给我们提供了一种默认的Item删 ...
- Maven快照机制(SNAPSHOT)
文章转自 http://www.cnblogs.com/EasonJim/p/6852840.html 以下引用自https://ayayui.gitbooks.io/tutorialspoint-m ...
- ThreadGroup其实比ExecutorService更好
用java做抓取的时候免不了要用到多线程的了,因为要同时抓取多个网站或一条线程抓取一个网站的话实在太慢,而且有时一条线程抓取同一个网站的话也比较浪费CPU资源.要用到多线程的等方面,也就免不了对线程的 ...
- min-max 容斥
$\min - \max$ 容斥 Part 1 对于简单的$\min - \max$容斥有一般形式,表达为:$\max(S)=\sum\limits_{T\subseteq S}(-1)^{|T|-1 ...
- .NET Core 3.0 跟踪
Preview1: https://blogs.msdn.microsoft.com/dotnet/2018/12/04/announcing-net-core-3-preview-1-and-ope ...
- JQuery如何实现双击事件时不触发单击事件
单击和双击事件的执行顺序: 单击(click):mousedown,mouseout,click: 双击(dblclick):mousedown,mouseout,click , mousedown, ...