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 }

 

posted @ 2015-08-30 15:03  Deribs4  阅读(347)  评论(0编辑  收藏  举报