• 1045. Favorite Color Stripe (30)


    题目连接:https://www.patest.cn/contests/pat-a-practise/1045原题如下:

    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
    
    这道题查看了其他人的,才知道是利用LIS……,难点是怎样将信息提取出来构建一个新的序列,从而可以使用LIS。本题中应EVA最喜欢的color的order
    重新构成一个序列,进而使用LIS。
     1 #include<stdio.h>
     2 #include<vector>
     3 #include<iostream>
     4 #include<map>
     5 using namespace std;
     6 
     7 int main()
     8 {
     9     vector<int>d,input;
    10     map<int,int>favorite;
    11     int n,N,i,c,M,j;
    12     scanf("%d",&n);
    13     scanf("%d",&N);
    14     for (i=0;i<N;i++)
    15     {
    16         scanf("%d",&c);
    17         favorite[c]=i;
    18       //favorite.insert(make_pair(c,i));
    19     }
    20     scanf("%d",&M);
    21     for (i=0;i<M;i++)
    22     {
    23         scanf("%d",&c);
    24         map<int,int>::iterator it=favorite.find(c);
    25         if (it!=favorite.end())input.push_back(it->second);  //input中存储的是favorite序号大小,以此来形成LIS(序号之间可以直接比较大小)
    26     }
    27     int len=input.size(),mm=1;
    28     if (len==0){printf("0");return 0;}
    29     d.assign(len,1);
    30     for (i=1;i<len;i++)    //LIS核心代码
    31     {
    32         for (j=0;j<i;j++)
    33         {
    34             if (input[i]>=input[j] && d[i]<d[j]+1)
    35             {
    36                 d[i]=d[j]+1;
    37             }
    38         }
    39         mm=d[i]>mm?d[i]:mm;
    40     }
    41     printf("%d",mm);
    42     return 0;
    43 }
    
    
  • 相关阅读:
    27:单词翻转
    c++自制锁机程序--两行代码
    19:字符串移位包含问题
    18:验证子串
    23:二维数组回形遍历
    Java获取程序或项目路径的常用方法
    java 、Android 提交参数转码问题
    java 上传图片 打水印
    使用getGenericSuperclass()和getActualTypeArguments()将DAO做成泛型
    zoj 1010 Area【线段相交问题】
  • 原文地址:https://www.cnblogs.com/wuxiaotianC/p/6412576.html
Copyright © 2020-2023  润新知