zoukankan      html  css  js  c++  java
  • 1045 Favorite Color Stripe 动态规划

    1045 Favorite Color Stripe

    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
    本问题可以看做最长不下降子序列

     1 #include <iostream>
     2 #include <vector>
     3 #include <map>
     4 using namespace std;
     5 const int maxn = 100005;
     6 
     7 vector<int> list;
     8 int dp[maxn];
     9 map<int,int> order;
    10 
    11 int main(){
    12     int n,m,l,tmp;
    13     scanf("%d",&n);
    14     scanf("%d",&m);
    15     for(int i=1;i<=m;i++){
    16         scanf("%d",&tmp);
    17         order[tmp]=i;
    18     }
    19     scanf("%d",&l);
    20     for(int i=0;i<l;i++){
    21         scanf("%d",&tmp);
    22         if(order[tmp]){
    23             list.push_back(order[tmp]);
    24         }
    25     }
    26     int ans=0;
    27     for(int i=0;i<list.size();i++){
    28         dp[i]=1;
    29         for(int j=0;j<i;j++){
    30             if(list[j]<=list[i]&&dp[i]<dp[j]+1){
    31                 dp[i]=dp[j]+1;
    32             }
    33         }
    34         ans=max(ans,dp[i]);
    35     }
    36     printf("%d",ans);
    37 }
    38 
    39

    另外还可以用DFS,但是会有两个点超时

     1 #include <iostream>
     2 #include <vector>
     3 #include <map>
     4 #include <set>
     5 using namespace std;
     6 const int maxm=250;
     7 const int maxn=250;
     8 
     9 int n,m,l;
    10 map<int,int> Mp,checkMap;
    11 int favor[maxn];
    12 vector<int> list;
    13 vector<int> path,tmppath;
    14 set<int> checkSet[maxn];
    15 int maxl=0;
    16 
    17 void DFS(int v,int indx){
    18     tmppath.push_back(v);
    19     if(indx==list.size()-1){
    20         if(tmppath.size()>maxl){
    21             maxl=tmppath.size();
    22         }
    23         tmppath.pop_back();
    24         return;
    25     }
    26     for(int i=indx+1;i<list.size();i++){
    27         if(checkSet[v].find(list[i])==checkSet[v].end()){
    28             DFS(list[i], i);
    29         }
    30     }
    31     tmppath.pop_back();
    32     return;
    33 }
    34 
    35 int main(){
    36     int tmp;
    37     scanf("%d",&n);
    38     scanf("%d",&m);
    39     for(int i=0;i<m;i++){
    40         scanf("%d",&favor[i]);
    41         Mp[favor[i]]=1;
    42     }
    43     for(int i=1;i<m;i++){
    44         checkSet[favor[i]]=checkSet[favor[i-1]];
    45         checkSet[favor[i]].insert(favor[i-1]);
    46     }
    47     //printf("%lu
    ",checkSet[favor[4]].size());
    48     scanf("%d",&l);
    49     for(int i=0;i<l;i++){
    50         scanf("%d",&tmp);
    51         if(Mp[tmp]) list.push_back(tmp);
    52     }
    53     
    54     for(int i=0;i<list.size();i++){
    55         DFS(list[i],i);
    56     }
    57     printf("%d",maxl);
    58 }
  • 相关阅读:
    用户登录
    open >>>文件操作
    随机数
    内置函数
    函数练习
    函数的动态参数
    函数的指定参数
    函数的普通参数
    函数的初步认识
    copy
  • 原文地址:https://www.cnblogs.com/lokwongho/p/9993618.html
Copyright © 2011-2022 走看看