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 (<=200) 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 (<=200) followed by M Eva's favorite color numbers given in her favorite order. Finally the third line starts with a positive integer L (<=10000) which is the length of the given stripe, followed by L colors on the stripe. All the numbers in a line are 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<cstdio>
- #include<algorithm>
- #include<cstring>
- using namespace std;
- const int maxn = ;
- const int maxc = ;
- int hashTable[maxn];
- int A[maxc],dp[maxc]; //数组A和dp的大小和L有关
- int main(){
- int n,m,x;
- memset(hashTable,-,sizeof(hashTable));
- scanf("%d%d",&n,&m);
- for(int i = ; i < m; i++){
- scanf("%d",&x);
- hashTable[x] = i;
- }
- int L,num = ;
- scanf("%d",&L);
- for(int i = ; i < L; i++){
- scanf("%d",&x);
- if(hashTable[x] >= ){
- A[num++] = hashTable[x];
- }
- }
- int ans = -;
- for(int i = ; i < num; i++){
- dp[i] = ;
- for(int j = ; j < i; j++){
- if(A[i] >= A[j] && dp[i] < dp[j] + ){
- dp[i] = dp[j] + ;
- }
- }
- ans = max(ans,dp[i]);
- }
- printf("%d",ans);
- return ;
- }
- //最长公共子序列
- #include<cstdio>
- #include<cstring>
- #include<algorithm>
- using namespace std;
- const int maxn = ;
- const int maxL = ;
- int dp[maxL][maxL],A[maxn],B[maxL];
- int main(){
- int n,m,x;
- scanf("%d%d",&n,&m);
- for(int i = ; i <= m; i++){ //[1,n]
- scanf("%d",&A[i]);
- }
- int L;
- scanf("%d",&L);
- for(int i = ; i <= L; i++){ //[1,L]
- scanf("%d",&B[i]);
- }
- for(int i = ; i <= m; i++){ //[0,m]
- dp[i][] = ;
- }
- for(int i = ; i <= L; i++){ //[0,L] 四个取值范围存疑
- dp[][i] = ;
- }
- for(int i = ; i <= m; i++){
- for(int j = ; j <= L; j++){
- int MAXV = max(dp[i-][j],dp[i][j-]);
- if(A[i] == B[j]){
- dp[i][j] = MAXV+;
- }else{
- dp[i][j] = MAXV;
- }
- }
- }
- printf("%d",dp[m][L]);
- return ;
- }
1045 Favorite Color Stripe (30)的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 【PAT甲级】1045 Favorite Color Stripe (30 分)(DP)
题意: 输入一个正整数N(<=200),代表颜色总数,接下来输入一个正整数M(<=200),代表喜爱的颜色数量,接着输入M个正整数表示喜爱颜色的编号(同一颜色不会出现两次),接下来输入一个 ...
- PAT (Advanced Level) 1045. Favorite Color Stripe (30)
最长公共子序列变形. #include<iostream> #include<cstring> #include<cmath> #include<algori ...
- 1045 Favorite Color Stripe 动态规划
1045 Favorite Color Stripe 1045. Favorite Color Stripe (30)Eva is trying to make her own color strip ...
- 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. ...
随机推荐
- Spring Cloud Alibaba学习笔记(22) - Nacos配置管理
目前业界流行的统一配置管理中心组件有Spring Cloud Config.Spring Cloud Alibaba的Nacos及携程开源的Apollo,本文将介绍Nacos作为统一配置管理中心的使用 ...
- HDFS-HA高可用集群搭建
HA高可用集群搭建 1.总体集群规划 在hadoop102.hadoop103和hadoop104三个节点上部署Zookeeper. hadoop102 hadoop103 hadoop104 Nam ...
- Python之TensorFlow的(案例)验证码识别-6
一.这里的案例相对比较简单,主要就是通过学习验证码的识别来认识深度学习中我们一般在工作中,需要处理的东西会存在哪些东西. 二.因为我没有数据集,没有关系,这里自己写了一个数据集,来做测试,为了方便我把 ...
- 3.怪异盒模型box-sizing?弹性盒模型|盒布局?【HTML】
在标准模式下的盒模型:盒子总宽度/高度=width/height+padding+border+margin 在怪异模式下的盒模型下,盒子的总宽度和高度是包含内边距padding和边框border宽度 ...
- 【译】Python数据结构
本章将更详细地描述您已经学到的一些内容,并添加了一些新的内容. 5.1 关于列表的更多内容 列表数据类型有一些更多的方法. 以下是列表对象的所有方法: list.append(x) 将项目添加到列表的 ...
- 原子性atomic/nonatomic
原子性:并发编程中确保其操作具备整体性,系统其它部分无法观察到中间步骤,只能看到操作前后的结果. 决定编译器生成的getter和setter是否原子(natomic)操作. i 因此,atomic ...
- 一段让人瑟瑟发抖的ABAP代码
昨天11月1日是万圣节,Jerry在继续忙着调研SAP Commerce Cloud里的产品主数据管理.晚上回家到SAP国外的社交媒体上一看,好热闹啊.国外的SAP从业者们纷纷以各种各样的方式庆祝万圣 ...
- c# Group类
- Redis配置文件详情
# Redis 配置文件 # 当配置中需要配置内存大小时,可以使用 1k, 5GB, 4M 等类似的格式,其转换方式如下(不区分大小写) # # 1k => bytes # 1kb => ...
- pip 和pip3的区别
前言装完python3后发现库里面既有pip也有pip3,不知道它们的区别,因此特意去了解了一下. 解释先搜索了一下看到了如下的解释, 安装了python3之后,库里面既会有pip3也会有pip 1. ...