1045. Favorite Color Stripe (30)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

提交代码

注意以后不要用动态申请内存,直接申请。

核心思想:对于的当前的颜色A条纹,找到颜色优先权比A大并且当前累积条纹个数最大的颜色B条纹(B可以等于A),然后颜色A条纹的个数=颜色B条纹的个数+1(即颜色A条纹的前面最后剪放颜色B条纹)。最后取颜色A条纹的最大值,即为符合条件的最大条纹个数。

 #include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
#include<vector>
#include<cmath>
#include<string>
#include<map>
#include<set>
using namespace std;
/*struct node{
int v,fr;//fr前面有多少个比他大的数
};*/
map<int,int> getorder;
int colornum[];//前一种颜色条纹当前的数量
int order[];//题目给出的颜色优先权
//只要考虑优先权不小于当前颜色的颜色B条纹数量的最大值,那么直接从B跳到当前颜色A条纹,当前颜色A的数量=B的数量+1
int main(){
//freopen("D:\\INPUT.txt","r",stdin);
int n,m,l,i,j;
scanf("%d",&n);
//int *colornum=new int[n+5];//前一种颜色当前的数量
//memset(colornum,0,sizeof(colornum));
/*for(i=0;i<=n;i++){
cout<<"i: "<<i<<" "<<colornum[i]<<endl;
}*/
scanf("%d",&m);
//int *order=new int[m+1];//题目给出的颜色优先权
for(i=;i<=m;i++){
scanf("%d",&order[i]);
getorder[order[i]]=i;//由颜色得到优先顺序,然后当前颜色条纹个数=max{之前所有颜色条纹个数,当前颜色条纹个数}+1;
}
scanf("%d",&l);
int maxlen=-,color;
//node *stripe=new node[l+1];//放题目给出的颜色
for(i=;i<=l;i++){
scanf("%d",&color);//当前的颜色
if(!getorder.count(color)){//注意不在队列中的颜色不进行分析
continue;
}
//只要考虑优先权不小于当前颜色的颜色A数量的最大值,那么直接从A跳到当前颜色,当前颜色的数量=A的数量+1
int curnum=getorder[color],maxnum=;
for(j=;j<=curnum;j++){
if(colornum[order[j]]>colornum[order[maxnum]]){
maxnum=j;
}
}
colornum[order[curnum]]=colornum[order[maxnum]]+;//加上当前的颜色
if(colornum[order[curnum]]>maxlen){
maxlen=colornum[order[curnum]];
/* cout<<"maxnum: "<<maxnum<<endl;
cout<<"color: "<<order[curnum]<<endl;
cout<<"maxlen: "<<maxlen<<endl;*/
}
}
printf("%d",maxlen);
return ;
}

pat1045. Favorite Color Stripe (30)的更多相关文章

  1. PAT-1045. Favorite Color Stripe (30)-LIS

    将Eva喜欢的颜色按顺序编个优先级, 2 3 1 5 6-> 1 2 3 4 5 然后读取stripe,将Eva不喜欢的先剔除掉,剩下的颜色替换为相应的优先级 2 2 4(去掉) 1 5 5 6 ...

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

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

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

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

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

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

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

  9. PAT (Advanced Level) 1045. Favorite Color Stripe (30)

    最长公共子序列变形. #include<iostream> #include<cstring> #include<cmath> #include<algori ...

随机推荐

  1. java8之lambda表达式&方法引用(一)

    本文将简单的介绍一下Lambda表达式和方法引用,这也是Java8的重要更新,Lambda表达式和方法引用最主要的功能是为流(专门负责迭代数据的集合)服务. 什么是lambda表达式 可以把lambd ...

  2. Oauth Client Credentials Grant

    http://www.cnblogs.com/dudu/p/4569857.html OAuth真是一个复杂的东东,即使你把OAuth规范倒背如流,在具体实现时也会无从下手.因此,Microsoft. ...

  3. Promise 异步函数顺序执行

    可以满足需求,且使用方法和Promise.all统一 var a = function() { return new Promise(function(resolve, reject) { setTi ...

  4. 787. Cheapest Flights Within K Stops

    There are n cities connected by m flights. Each fight starts from city u and arrives at v with a pri ...

  5. 【机器学习】李宏毅机器学习-Keras-Demo-神经网络手写数字识别与调参

    参考: 原视频:李宏毅机器学习-Keras-Demo 调参博文1:深度学习入门实践_十行搭建手写数字识别神经网络 调参博文2:手写数字识别---demo(有小错误) 代码链接: 编程环境: 操作系统: ...

  6. CI框架源码学习笔记4——Benchmark.php

    我们回到Codeigniter.php上继续往下看,第一个引入的类文件是Benchmark.php,这个文件主要是提供基准测试,具体使用方法参考手册http://codeigniter.org.cn/ ...

  7. MPI环境配置

    单机多核配置:https://www.cnblogs.com/shixiangwan/p/6626156.html 多计算机配置:https://blog.csdn.net/WASEFADG/arti ...

  8. java 实现导出Excel文件

    java 实现导出Excel(java生成 excel 并导出文件) 经常有有一些数据需要导出成   excel  格式 ,所以就需要实现啦 开始: 1.加入jar poi-3.6-20091214. ...

  9. python 中文路径问题

    Python直接读取中文路径的文件时失败,可做如下处理: inpath = 'D:/work/yuanxx/在线导航/驾车导航/walk_log/20130619_172355.txt' uipath ...

  10. JavaScript权威指南--闭包讲解摘记

    不积跬步无以至千里,不积小流无以成江河. 关于闭包的解释,在<JavaScript权威指南>中讲的很透彻了.今天看了书中的一个段讲解,更加深了对闭包的理解,特此记下,以备查阅. 在同一个作 ...