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的更多相关文章

  1. PAT甲级1045. Favorite Color Stripe

    PAT甲级1045. Favorite Color Stripe 题意: 伊娃正在试图让自己的颜色条纹从一个给定的.她希望通过剪掉那些不必要的部分,将其余的部分缝合在一起,形成她最喜欢的颜色条纹,以保 ...

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

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

  4. PAT 甲级 1045 Favorite Color Stripe(DP)

    题目链接 Favorite Color Stripe 题意:给定$A$序列和$B$序列,你需要在$B$序列中找出任意一个最长的子序列,使得这个子序列也是$A$的子序列 (这个子序列的相邻元素可以重复) ...

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

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

  7. 1045 Favorite Color Stripe 动态规划

    1045 Favorite Color Stripe 1045. Favorite Color Stripe (30)Eva is trying to make her own color strip ...

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

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

随机推荐

  1. 简明 Vim 练级攻略------转自陈皓coolshell

    vim的学习曲线相当的大(参看各种文本编辑器的学习曲线),所以,如果你一开始看到的是一大堆VIM的命令分类,你一定会对这个编辑器失去兴趣的.下面的文章翻译自<Learn Vim Progress ...

  2. 接上篇elasticsecrchi 进行搜索及时提示,数据库以及后台代码

    -- ------------------------------ Table structure for articles-- ----------------------------DROP TA ...

  3. Ros使用Arduino 3用rosserial创建一个subscriber

    在前面的一节中,我们已经使用arduino创建了一个publisher节点,接下来将会用arduino来创建一个subscriber,接收电脑传去的信息并做出相应的反应. 1启动Arduino 将ar ...

  4. M100 对频

  5. UCML 参与者关键 与依赖关联外键

  6. MetaMask/obs-store

    https://github.com/MetaMask/obs-store ObservableStore ObservableStore is a synchronous in-memory sto ...

  7. metamask中的import account的代码实现

    metamask-extension/app/scripts/account-import-strategies/index.js 这部分就是用户如果往metamask中import一个已有的账户调用 ...

  8. 【NOIP2017 D1T3】逛公园

    NOIP2017 D1T3 逛公园 题意:给一个有向图,每条边有权值,问从\(1\)到\(N\)的长度不超过最短路长度\(+K\)的路径条数.如果有无数条则输出\(-1\). 思路:我们首先扔掉\(- ...

  9. 学习CSS布局 - position例子

    position例子 通过具体的例子可以帮助我们更好地理解“position”.下面是一个真正的页面布局. 结果: 代码如下: <!DOCTYPE html> <html lang= ...

  10. day86

    视图组件 基于以往我们所用的视图函数,我们发现其中冗余的代码比较多,今天就来对其进行封装,争取做一个代码洁癖者 原来我们的视图函数: class Book(APIView): def get(self ...