zoukankan      html  css  js  c++  java
  • PAT.1045 Favorite Color Stripe(LIS变种)

    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 (≤) 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



    居然在刷PAT的时候碰到了dp,我的天哪....
    这题目,读着读着就有点dp的味道,可惜了大半年没做dp题目的我一眼没相中这道题。。。
    题目就是要求一个序列中按照一定顺序排列的数字最多有多少个,稍加思索(绞尽脑汁)就(不是)知道把要求的模式序列映射成大小关系,然后直接在
    目标序列里按照最长不下降子序列找就行了。

     1 #include <iostream>
     2 #include <algorithm>
     3 using namespace std;
     4 
     5 const int maxn = 10000 + 5;
     6 
     7 int Hash[200 + 5];
     8 
     9 int colors[maxn];
    10 
    11 int dp[maxn];
    12 
    13 int main() {
    14     int n, m, l, favorites;
    15     cin >> n >> m;
    16     for(int i = 1; i <= m; i ++) {
    17         cin >> favorites;
    18         Hash[favorites] = i;
    19     }
    20     cin >> l;
    21     int temp, cnt = 0;
    22     for(int i = 1; i <= l; i ++) {
    23         cin >> temp;
    24         if(Hash[temp] > 0) {
    25             colors[++ cnt] = Hash[temp];
    26         }
    27     }
    28     int ans = 0;
    29     for(int i = 1; i <= cnt; i ++) {
    30         dp[i] = 1;
    31         for(int j = 1; j < i; j ++) {
    32             if(colors[i] >= colors[j]) {
    33                 dp[i] = max(dp[i], dp[j] + 1);
    34             }
    35         }
    36         ans = max(dp[i], ans);
    37     }
    38     
    39     cout << ans << endl;
    40     return 0;
    41 }
     
  • 相关阅读:
    双十一脱单就靠它:创维小湃蓝牙音箱体验评测
    专注产品真正价值:iWALK真无线蓝牙耳机体验评测
    美好的童年伙伴:360 智能儿童手表 P1体验评测
    前端也要学系列:设计模式之装饰者模式
    前端也要学系列:设计模式之策略模式
    你不知道的Javascript:有趣的setTimeout
    接受”不完美“:分布式事务学习总结
    如何进行团队技术分享
    又是一年寒冬时
    mybatis ~ 批量更新(sql循环)update foreach
  • 原文地址:https://www.cnblogs.com/bianjunting/p/12977331.html
Copyright © 2011-2022 走看看