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. C#之Socket断线重连

    一.网上常用方法 1.当Socket.Conneted == false时,调用如下函数进行判断 /// /// 当socket.connected为false时,进一步确定下当前连接状态 /// / ...

  2. C++: I/O流详解

    一.输入流操作 1.read 无格式输入指定字节数 istream& read ( char* pch, int nCount ); istream& read ( unsigned ...

  3. CHNS类

    NS类集合介绍 1.常用部分 NSDictionary NSString NSArray 数组 NSTimer 定时器 NSRange 范围 NSNotification 2.网络相关 NSURLCo ...

  4. c语言参考书籍

    很惭愧没能把c++学的很好,毕竟离开始工作只有2年时间,对自己要求不要过高,慢慢来吧.话说知道自己的不足,以后要更加抓紧了!fighting~ 现在计划着把c语言给学习一下了,当然这次指的是深入地学习 ...

  5. Python之图片缩放功能实现

    这几天由于有项目在做,自己的学习部分然后没有很充足的时间,但是这些零碎的时间也是很宝贵的,所以还是继续学我的python,我很喜欢这个语言,因为简洁,开发环境简单,更多的事,功能灰常的强大,所以好多有 ...

  6. Miller-Rabin素数测试

    Miller-Rabin素数测试 给出一个小于1e18的数,问它是否为质数?不超过50组询问.hihocoder 我是真的菜,为了不误导他人,本篇仅供个人使用. 首先,一个1e18的数,朴素\(O(\ ...

  7. luogu p4174 最大获利(最大权闭合子图)

    luogu p4174 最大获利(最大权闭合子图) 给定n个点,m条边,每条边有一个贡献,每个点有一个代价.选择一条边,会付出边所连两个点的代价,问最大代价. 我们换个建图方式:把图G中的边\(e_i ...

  8. 设置django在linux后台运行&查看端口使用

    1.后台运行(&),允许所有用户(0.0.0.0)访问,端口为8888 nohup python manage.py runserver 0.0.0.0 8888 & 2.由端口号88 ...

  9. kuangbin专题十六 KMP&&扩展KMP HDU1238 Substrings

    You are given a number of case-sensitive strings of alphabetic characters, find the largest string X ...

  10. (16)break和continue

    #(1)这里顺带说下pass # pass 过 的意思,防止代码报错,就是个占位的, if True: # 不允许代码块里面的内容为空,用pass占位 pass #(2)break (只能用在循环当中 ...