pat1045. Favorite Color Stripe (30)

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条纹的最大值,即为符合条件的最大条纹个数。

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<cstring>
 5 #include<queue>
 6 #include<vector>
 7 #include<cmath>
 8 #include<string>
 9 #include<map>
10 #include<set>
11 using namespace std;
12 /*struct node{
13     int v,fr;//fr前面有多少个比他大的数
14 };*/
15 map<int,int> getorder;
16 int colornum[205];//前一种颜色条纹当前的数量
17 int order[205];//题目给出的颜色优先权
18 //只要考虑优先权不小于当前颜色的颜色B条纹数量的最大值,那么直接从B跳到当前颜色A条纹,当前颜色A的数量=B的数量+1
19 int main(){
20     //freopen("D:\\INPUT.txt","r",stdin);
21     int n,m,l,i,j;
22     scanf("%d",&n);
23     //int *colornum=new int[n+5];//前一种颜色当前的数量
24     //memset(colornum,0,sizeof(colornum));
25     /*for(i=0;i<=n;i++){
26         cout<<"i:  "<<i<<"  "<<colornum[i]<<endl;
27     }*/
28     scanf("%d",&m);
29     //int *order=new int[m+1];//题目给出的颜色优先权
30     for(i=1;i<=m;i++){
31         scanf("%d",&order[i]);
32         getorder[order[i]]=i;//由颜色得到优先顺序,然后当前颜色条纹个数=max{之前所有颜色条纹个数,当前颜色条纹个数}+1;
33     }
34     scanf("%d",&l);
35     int maxlen=-1,color;
36     //node *stripe=new node[l+1];//放题目给出的颜色
37     for(i=1;i<=l;i++){
38         scanf("%d",&color);//当前的颜色
39         if(!getorder.count(color)){//注意不在队列中的颜色不进行分析
40             continue;
41         }
42         //只要考虑优先权不小于当前颜色的颜色A数量的最大值,那么直接从A跳到当前颜色,当前颜色的数量=A的数量+1
43         int curnum=getorder[color],maxnum=1;
44         for(j=2;j<=curnum;j++){
45             if(colornum[order[j]]>colornum[order[maxnum]]){
46                 maxnum=j;
47             }
48         }
49         colornum[order[curnum]]=colornum[order[maxnum]]+1;//加上当前的颜色
50         if(colornum[order[curnum]]>maxlen){
51             maxlen=colornum[order[curnum]];
52 /*            cout<<"maxnum: "<<maxnum<<endl;
53             cout<<"color: "<<order[curnum]<<endl;
54             cout<<"maxlen: "<<maxlen<<endl;*/
55         }
56     }
57     printf("%d",maxlen);
58     return 0;
59 }
时间: 2024-08-11 14:15:33

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

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

PAT1045. Favorite Color Stripe

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

1045. Favorite Color Stripe (30)

LCS 最大公共子序列. memset函数需要include <string.h> 时间限制 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 cut

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 favorite colors in her favorite order by cutting off those unwanted pieces and sewing the remaining parts together to form her favorite color stripe.

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

题意: 输入一个正整数N(<=200),代表颜色总数,接下来输入一个正整数M(<=200),代表喜爱的颜色数量,接着输入M个正整数表示喜爱颜色的编号(同一颜色不会出现两次),接下来输入一个正整数L(<=10000),代表条带的长度,接着输入L个正整数表示条带上的颜色的编号.输出以喜爱颜色顺序排列的最长子串长度(不必每种颜色都有,只保证相对位置相同,同种颜色可连续). 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>

codeforces219C - Color Stripe DP+贪心

题意:给你n,k,大意就是说给你一个已经涂满颜色长为n的字符串,现有k种颜色可以选择,问你最少要改变多少个箱子的颜色使得相邻箱子之间颜色不同. 解题思路:当k = 2 时单独讨论,不能用贪心,其余情况都可贪心得到. 解题代码: 1 // File Name: 219c.cpp 2 // Author: darkdream 3 // Created Time: 2014年07月26日 星期六 15时45分56秒 4 5 #include<vector> 6 #include<list>

CodeForces 219C Color Stripe

Color Stripe Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 219C Description A colored stripe is represented by a horizontal row of n square cells, each cell is pained one of k colors.

贪心 Codeforces Round #135 (Div. 2) C. Color Stripe

题目传送门 1 /* 2 贪心:当m == 2时,结果肯定是ABABAB或BABABA,取最小改变量:当m > 2时,当与前一个相等时, 改变一个字母 3 同时不和下一个相等就是最优的解法 4 */ 5 #include <cstdio> 6 #include <cstring> 7 #include <algorithm> 8 using namespace std; 9 10 const int MAXN = 5e5 + 10; 11 const int IN

1045 Favorite Color Stripe (最长不下降子序列 LIS)

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