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. Nginx入门(二)——双机热备

    upstream backend { server ; server backup; } server { listen ; server_name localhost; #charset koi8- ...

  2. P2P技术之STUN、TURN、ICE详解

    现在大多数计算机主机都位于防火墙或NAT之后,很少有计算机直接接入Internet.通常,人们希望网络中两天计算机能直接进行通信(P2P通信),而不是需要其他公共服务器的中转. 由于主机位于防火墙或N ...

  3. LOJ 2452 对称 Antisymmetry——用hash求回文串数

    概念 用hash求最长回文串/回文串数 首先,易知,回文串具有单调性. 如果字符串 $s[l...r]$ 为回文串串,那么 $s[x...y]$($l < x, y < r$ 且 $|l- ...

  4. 智能灯控(基于ZigBee)

    时间:2017年12月 阶段:大二上学期 背景:单片机原理与应用课设 名称:智能灯控 摘要 本系统实现了多方式控灯功能,有按键控灯.串口指令控灯.点对点无线射频控灯.AI模式控灯.其中AI模式控灯是通 ...

  5. CSS 布局和 BFC

    什么是 BFC 在一个Web页面的CSS渲染中,块级格式化上下文 (Block Fromatting Context)是按照块级盒子布局的.W3C对BFC的定义如下:浮动元素和绝对定位元素,非块级盒子 ...

  6. qsing

    qsing1 1.低仿机器人 一道大模拟 2.放爆竹 小辉原本想让小明告诉他,如果同时点燃n串雷,最多会有多长的时间至少有两串雷爆炸的声音是一样的. 但是小辉觉得这个问题真是太简单了,所以决定问小明, ...

  7. aws使用之负载均衡elb要点

    目录: 1.创建负载 2.目标群组 3.使用注意事项 4.总结 创建负载 alb负载 https://docs.aws.amazon.com/zh_cn/elasticloadbalancing/la ...

  8. Spring Cloud Gateway(十):网关过滤器工厂 GatewayFilterFactory

    本文基于 spring cloud gateway 2.0.1 1.GatewayFilterFactory 简介 路由过滤器允许以某种方式修改传入的HTTP请求或传出的HTTP响应. 路径过滤器的范 ...

  9. OpenJudge 1.5.36:计算多项式的值

    描述 假定多项式的形式为xn+xn-1+…+x2+x+1,请计算给定单精度浮点数x和正整数n值的情况下这个多项式的值. 输入输入仅一行,包括x和n,用单个空格隔开.x在float范围内,n <= ...

  10. Thift初探 (一)

    Maven pox.xml: <dependency> <groupId>org.apache.thrift</groupId> <artifactId> ...