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

题意:

给出m中颜色作为喜欢的颜色(同时也给出顺序),然后给出一串长度为L的颜色序列,现在要去掉这个序列中的不喜欢的颜色,然后求剩下序列的一个子序列,使得这个子序列表示的颜色顺序符合自己喜欢的颜色的顺序,不一定要所有喜欢的颜色都出现。

思路:

在线实现,没输入了一个颜色,如果是属于喜欢的颜色的话,就遍历这种喜欢的颜色之前的所有喜欢的颜色,如喜欢的颜色为2 3 1 5 6,已经输入的颜色序列是2 2 4 1 5 5 ,接下来输入了6,则6在喜欢的颜色里是最后一个,遍历前面的四个颜色2,3,1,5和它6自己,用mx计算这个6添在哪一个数字序列后面可使长度最长,更新dp[x]。

AC代码:

#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<string>
#include<cstring>
using namespace std;
int n,m,l;
int fav[];//喜欢的颜色序列
int x;
int dp[];//dp[x]表示以x结尾的 符合条件的最长长度
int v[];//v[x]表示 这个x在喜欢的颜色序列中是第几个,不在序列中则为0
int main()
{
cin>>n;
memset(,sizeof(v),);
memset(,sizeof(dp),);
cin>>m;
for(int i=;i<=m;i++){
cin>>fav[i];
v[fav[i]]=i;//fav[i]在序列中是第i个
}
cin>>l;
int maxl=;
for(int i=;i<=l;i++){
cin>>x;
int mx=;
for(int j=;j<=v[x];j++){//遍历喜欢的颜色的前几个颜色
mx=max(mx,dp[fav[j]]+);//挑出以x结尾的符合条件的最长长度=max(,前某种颜色的最长长度+1)
}
dp[x]=mx;//更新 就算新的mx比dp[x]小,也要更新为mx
maxl=max(dp[x],maxl);//更新总的最长长度
}
cout<<maxl<<endl;
return ;
}

PAT 甲级 1045 Favorite Color Stripe (30 分)(思维dp,最长有序子序列)的更多相关文章

  1. 1045 Favorite Color Stripe (30分)(简单dp)

    Eva is trying to make her own color stripe out of a given one. She would like to keep only her favor ...

  2. 【PAT甲级】1045 Favorite Color Stripe (30 分)(DP)

    题意: 输入一个正整数N(<=200),代表颜色总数,接下来输入一个正整数M(<=200),代表喜爱的颜色数量,接着输入M个正整数表示喜爱颜色的编号(同一颜色不会出现两次),接下来输入一个 ...

  3. PAT甲级1045. Favorite Color Stripe

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

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

  5. PAT 甲级 1045 Favorite Color Stripe

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

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

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

  7. 1045 Favorite Color Stripe (30)(30 分)

    Eva is trying to make her own color stripe out of a given one. She would like to keep only her favor ...

  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. matlab(2) Logistic Regression: 画出样本数据点plotData

    画出data数据 data数据 34.62365962451697,78.0246928153624,030.28671076822607,43.89499752400101,035.84740876 ...

  2. Mybatis-Generator逆向工程,复杂策略(Criteria拼接条件)

    基于上一篇修改 1.Generator配置文件修改,将targetRuntime改为MyBatis3 2.项目结构目录 这个xxxExample就是拼接条件用的 3.测试代码 注释写的很详细 publ ...

  3. 2019年java技术大盘点

    福州SEO:2019年互联网企业在Java开发中有哪些主流.热门的IT技术呢,下面让我们来看一下. 微服务技术 微服务架构主要有:Spring Cloud. Dubbo. Dubbox等,以 Dubb ...

  4. C#经纬度加减运算(度°分′秒″格式)

    经度是分和秒是按60进位,如果要做运算第一步就是转换成浮点数,之后就是计算和还原. using System.Text.RegularExpressions; public static double ...

  5. BZOJ 3636 教义问答手册 (分治)

    题意 一个整数数列,多次询问某段区间[li,ri][l_i,r_i][li​,ri​]内,选出若干个长度为LLL且不相交的连续段使选出来的数和最大. 分析 首先想朴素的区间DPDPDP 设f[i][j ...

  6. 入门: 在Windows上搭建Flutter开发环境

    使用镜像 由于在国内访问Flutter有时可能会受到限制,Flutter官方为中国开发者搭建了临时镜像,大家可以将如下环境变量加入到用户环境变量中: export PUB_HOSTED_URL=htt ...

  7. 34、[源码]-AOP原理-链式调用通知方法

    34.[源码]-AOP原理-链式调用通知方法

  8. 007——转载-MATLAB读取文件夹下的文件名

    (一)参考文献:https://blog.csdn.net/liutaojia/article/details/84899923 (二)第一步:获取文件夹下某类型数据的所有文件名 主要包括三个步骤: ...

  9. mysql修改default值

    ALTER TABLE xxxxx   ALTER COLUMN  xxxxx   SET DEFAULT '0';

  10. PostgreSQL 学习手册-模式Schema

    一个数据库包含一个或多个命名的模式,模式又包含表.模式还包含其它命名的对象,包括数据类型.函数,以及操作符.同一个对象名可以在不同的模式里使用而不会导致冲突: 比如,schema1和myschema都 ...