zoukankan      html  css  js  c++  java
  • PAT 1045 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 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 (≤) 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 (≤) followed by M Eva's favorite color numbers given in her favorite order. Finally the third line starts with a positive integer L (≤) which is the length of the given stripe, followed by L colors on the stripe. All the numbers in a line a 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 using namespace std;
     4 /*
     5   把该题转换为LIS问题, 就变得简单了
     6   此外在找LIS的时候, 应该排序不需要的颜色, 否则会导致错误
     7 */
     8 int main(){
     9   int n, i, j;
    10   scanf("%d", &n);
    11   vector<int> idx(n+1, -1);
    12   int cnt, color;
    13   scanf("%d", &cnt);
    14   for(i=0; i<cnt; i++){
    15     scanf("%d", &color);
    16     //数的大小不是按照数值来排列的而是通过输入的顺序来排列的, 后面输入的数大
    17     idx[color]=i;
    18   }
    19   scanf("%d", &cnt);
    20   vector<int> v(cnt+1), dp(cnt+1, 1);
    21   int ans=0, index=0, temp;
    22   for(i=0; i<cnt; i++){
    23     scanf("%d", &temp);
    24     if(idx[temp]!=-1) v[index++] = temp;
    25   }
    26   for(i=0; i<index; i++){
    27     for(j=0; j<i; j++){
    28       if(idx[v[i]]>=idx[v[j]] && dp[j]+1>dp[i])
    29         dp[i] = dp[j]+1;
    30     }
    31     if(ans<dp[i]) ans=dp[i];
    32   }
    33   printf("%d
    ", ans);
    34   return 0;
    35 }
  • 相关阅读:
    python—logger
    print默认在末尾添加一个换行符,但其实也可以不用!
    Python做int()强制类型转换的时候,小数是如何取舍的?
    Python基础之好玩的字符串格式化f-string格式
    【奔走相告】- Github送福利:用户可免费创建私有代码库啦
    我30岁了,转行学编程可以吗? 排除法告诉你答案
    Python基础之好玩的字符串格式化之类C风格
    Python基础之白话说函数
    变量 和 注释
    什么是编程语言,什么是Python解释器
  • 原文地址:https://www.cnblogs.com/mr-stn/p/9588010.html
Copyright © 2011-2022 走看看