pat1045. Favorite Color Stripe (30)
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
注意以后不要用动态申请内存,直接申请。
核心思想:对于的当前的颜色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)的更多相关文章
- 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 ...
- 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)
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 ...
随机推荐
- String的split(String regex, int limit)方法小结
split(String regex, int limit)方法,头一个参数String regex表示字符串分割的模式,包括分隔符和正则表达式:但是第二个参数limit比较迷糊人,api中这样解释: ...
- 工作中用的cobbler命令行
在使用cobbler服务器,从pxe启动虚机的时候,经常用到的cobbler命令行 1.查看注册信息 cobbler system report --name=test25 2.注册信息 cobble ...
- python的发音
我一直读的是:拍方(可能是受有道词典发音的影响了~),可是别人都听不懂,他们大多是读的拍森. 来看看下面这个小伙伴的解释,感觉他说的挺好: “θ”这个字符的发音,有 80% 的中国人(学英文的人)都读 ...
- 前端性能优化-gzip
为什么要开启GZIP 我们需要下载一个100KB的Javascript文件,正常的下载量就是100KB,如果我们把文件在服务端压缩一下,压缩成30kb,下载到客户端再进行解压,这样就减少了大量的HTT ...
- ipad中icon与launchimage的size
图标(AppIcon)与启动图(LaunchImage)是开发iOS应用程序必不可少的内容,但是在网络上对于这部分的内容讲解的并不详细,所以花些时间写了这篇文章,希望有需要的朋友可以随时查看 想知道A ...
- CF1100E Andrew and Taxi 二分答案+拓扑排序
\(\color{#0066ff}{ 题目描述 }\) 给定一个有向图,改变其中某些边的方向,它将成为一个有向无环图. 现在求一个改变边方向的方案,使得所选边边权的最大值最小. \(\color{#0 ...
- P3398 仓鼠找sugar 树上路径相交判断
\(\color{#0066ff}{题目描述}\) 小仓鼠的和他的基(mei)友(zi)sugar住在地下洞穴中,每个节点的编号为1~n.地下洞穴是一个树形结构.这一天小仓鼠打算从从他的卧室(a)到餐 ...
- 树链剖分【洛谷P3833】 [SHOI2012]魔法树
P3833 [SHOI2012]魔法树 题目描述 Harry Potter 新学了一种魔法:可以让改变树上的果子个数.满心欢喜的他找到了一个巨大的果树,来试验他的新法术. 这棵果树共有N个节点,其中节 ...
- Linux下的hosts文件和network文件区别
Linux下的hosts文件和network文件区别 Linux下有两种与计算机名相关的配置文件 1.hosts文件,路径:/etc/hosts,此文间是在网络上使用的, 用于解析计算机名 ...
- Hanlp(汉语言处理包)配置、使用、官方文档
配置使用教程:https://github.com/hankcs/HanLP Hanlp官方文档:http://www.hankcs.com/nlp/hanlp.html 参考API:http://h ...