zoukankan      html  css  js  c++  java
  • 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 }
    
    
  • 相关阅读:
    C语言学习趣事_19_C参考手册连接
    2_Windows下利用批处理文件获取命令行命令帮助信息
    C语言学习趣事_FILE_TYPE
    清华大学出版社版_Windows程序设计_方敏_不足_3
    Windows程序设计零基础自学_14_Windows文件和目录操作
    3_Windows下利用批处理文件_去除C源代码中指示行号的前导数字
    随想_7_Windows_7_Visual_Studio_2008_问题
    C语言小算法_1_数值转换
    C语言学习趣事_20_Assert_Setjmp
    C语言学习趣事_20_关于数组名与指针的讨论
  • 原文地址:https://www.cnblogs.com/wuxiaotianC/p/6412576.html
Copyright © 2011-2022 走看看