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

LIS——最长不下降子序列:
算法设计
定义一个长度为N的order数组,将Eva喜欢的颜色编号作为下标,对应元素为次序编号,例如给定喜欢的颜色为2 3 1 5 6,那么order[2]=1,order[3]=2……定义数组A存储给定的珠子序列,定义一个数组dp,其中dp[i]表示以 A[i]结尾的符合要求的子序列最长长度。那么对于0<=i<N,找出0<=j<i之间满足order[i]>=order[j]的最大的dp元素值MAX,则dp[i]=max(MAX,1)。遍历dp数组找出最大的值即为所求。

 #include <iostream>
using namespace std;
int like[], color[], dp[];
int main()
{
int n, m, k, res = , x, nums = ;
cin >> n >> m;
for(int i=;i<=m;++i)//给自己喜欢颜色排序标号
{
cin >> x;
like[x] = i;
}
cin >> k;
for (int i = ; i < k; ++i)
{
cin >> x;
if (like[x] > )//删除不是喜欢颜色的变量
color[nums++] = like[x];//记住该颜色的标号
}
for (int i = ; i < nums; ++i)
{
dp[i] = ;//每种颜色单独拼接的长度为1
for (int j = ; j < i; ++j)
if (color[j] <= color[i])//只要前面的颜色的序号比自己小,那么就可以和直接拼接
dp[i] = dp[i] > (dp[j] + ) ? dp[i] : (dp[j] + );
res = res > dp[i] ? res : dp[i];
}
cout << res;
return ;
}

LCS——最长公共子序列
算法设计
定义一个数组A储存喜欢的颜色编号序列,定义一个数组B存储给定的一串珠子序列,找出A、B之间的最长公共子序列即可。由于颜色可以重复,即子序列中可以有重复元素,状态转移方程为:

如果A[i]==B[j],

如果A[i]!=B[j],

边界情况为:

dp[i][0]=dp[0][j]=0(0<=i<=M,0<=j<=L)

 using namespace std;
int dp[][];
int main(){
int N,M,L;
scanf("%d%d",&N,&M);
int A[M+]={};
for(int i=;i<=M;++i)
scanf("%d",&A[i]);
scanf("%d",&L);
int B[L+]={};
for(int i=;i<=L;++i)
scanf("%d",&B[i]);
for(int i=;i<=M;++i)
for(int j=;j<=L;++j)
if(A[i]==B[j])
dp[i][j]=max(dp[i][j-],dp[i-][j-])+;
else
dp[i][j]=max(dp[i][j-],dp[i-][j]);
printf("%d",dp[M][L]);
return ;
}

PAT甲级——A1045 Favorite Color Stripe的更多相关文章

  1. PAT甲级1045. Favorite Color Stripe

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

  2. PAT 甲级 1045 Favorite Color Stripe

    https://pintia.cn/problem-sets/994805342720868352/problems/994805437411475456 Eva is trying to make ...

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

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

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

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

  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. PAT甲级题解(慢慢刷中)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  8. 【转载】【PAT】PAT甲级题型分类整理

    最短路径 Emergency (25)-PAT甲级真题(Dijkstra算法) Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS) Travel P ...

  9. PAT 甲级真题题解(1-62)

    准备每天刷两题PAT真题.(一句话题解) 1001 A+B Format  模拟输出,注意格式 #include <cstdio> #include <cstring> #in ...

随机推荐

  1. scrapy - 给scrapy 的spider 传值

    scrapy - 给scrapy 的spider 传值 方法一: 在命令行用crawl控制spider爬取的时候,加上-a选项,例如: scrapy crawl myspider -a categor ...

  2. 16.ajax_case05

    # 抓取36氪快讯 # https://36kr.com/newsflashes import requests import json header = { 'Accept': 'text/html ...

  3. Java中9大内置基本数据类型Class实例和数组的Class实例

    1.Java中9大内置几本数据类型: 对于对象来说,可以直接使用对象.getClass()或者Class.forName(className);.类名.class都可以获取Class实例. 但是我们的 ...

  4. css盒模型问题

    css盒模型问题 1.基本概念:标准模型和ie模型 2.标准模型和ie模型的区别 3.css如果设置这两种模型 4.js如何获取盒模型的宽高 5.边距重叠 6.BFC 1.CSS盒模型本质上是一个盒子 ...

  5. JS数组 谁是团里成员(数组赋值)var myarray = new Array(66,80,90,77,59);//创建数组同时赋值

    谁是团里成员(数组赋值) 数组创建好,接下来我们为数组赋值.我们把数组看似旅游团的大巴车,大巴车里有很多位置,每个位置都有一个号码,顾客要坐在哪个位置呢? 第一步:组个大巴车 第二步:按票对号入座 大 ...

  6. Python全栈开发:进程代码实例

    进程与线程的关系 #!/usr/bin/env python # -*- coding;utf-8 -*- """ 多进程(主进程,子进程): 优点:能同时利用多个CPU ...

  7. Linux时间和时区设定

    一.时区设定 由于安装系统时采用了UTC,那么什么是UTC呢,简单的说UTC就是0时区的时间,是国际标准,而中国处于UTC+8时区. 使用tzselect命令,过程如下: 可以看到此环境变量已设置,将 ...

  8. Loadrunner学习---脚本编写(1)

    Loadrunner学习---脚本编写(1) 中午看了两集<奋斗>发现越看越想看,但是想到好不容易没上班,在家还是赶紧学习下LR的知识吧.下面这个网页的文章原来也是看过的,但发现没几天就忘 ...

  9. es6 + 笔记整理

    1. ES6提供了默认参数值机制,允许你为参数设置默认值,防止在函数被调用时没有传入这些参数: const required = () => {throw new Error('Missing ...

  10. 廖雪峰Java13网络编程-2Email编程-1发送email

    1.邮件发送 1.1传统邮件发送: 传统的邮件是通过邮局投递,从一个邮局到另一个邮局,最终到达用户的邮箱. 1.2电子邮件发送: 与传统邮件类似,它是从用户电脑的邮件软件(如outlook)发送到邮件 ...