zoukankan      html  css  js  c++  java
  • PAT 甲级 1045 Favorite Color Stripe (30 分)(思维dp,最长有序子序列)

    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

    题意:

    给出m中颜色作为喜欢的颜色(同时也给出顺序),然后给出一串长度为L的颜色序列,现在要去掉这个序列中的不喜欢的颜色,然后求剩下序列的一个子序列,使得这个子序列表示的颜色顺序符合自己喜欢的颜色的顺序,不一定要所有喜欢的颜色都出现。

    思路:

          在线实现,没输入了一个颜色,如果是属于喜欢的颜色的话,就遍历这种喜欢的颜色之前的所有喜欢的颜色,如喜欢的颜色为2 3 1 5 6,已经输入的颜色序列是2 2 4 1 5 5 ,接下来输入了6,则6在喜欢的颜色里是最后一个,遍历前面的四个颜色2,3,1,5和它6自己,用mx计算这个6添在哪一个数字序列后面可使长度最长,更新dp[x]。

    AC代码:

    #include<iostream>
    #include<algorithm>
    #include<vector>
    #include<queue>
    #include<map>
    #include<string>
    #include<cstring>
    using namespace std;
    int n,m,l;
    int fav[205];//喜欢的颜色序列
    int x;
    int dp[205];//dp[x]表示以x结尾的 符合条件的最长长度
    int v[205];//v[x]表示 这个x在喜欢的颜色序列中是第几个,不在序列中则为0
    int main()
    {
        cin>>n;
        memset(0,sizeof(v),0);
        memset(0,sizeof(dp),0);
        cin>>m;
        for(int i=1;i<=m;i++){
            cin>>fav[i];
            v[fav[i]]=i;//fav[i]在序列中是第i个
        }
        cin>>l;
        int maxl=0;
        for(int i=1;i<=l;i++){
            cin>>x;
            int mx=0;
            for(int j=1;j<=v[x];j++){//遍历喜欢的颜色的前几个颜色    
                mx=max(mx,dp[fav[j]]+1);//挑出以x结尾的符合条件的最长长度=max(,前某种颜色的最长长度+1)
            }
            dp[x]=mx;//更新 就算新的mx比dp[x]小,也要更新为mx
            maxl=max(dp[x],maxl);//更新总的最长长度
        }
        cout<<maxl<<endl;
        return 0;
    }
  • 相关阅读:
    Java实现八大排序算法
    Java实现二分查找算法
    Win10下通过IIS调试ASP程序遇到的问题和解决方案
    Nginx几种负载均衡算法及配置实例
    Java解决CSRF问题
    Nginx Https配置不带www跳转www
    面试中的Https
    面试中的DNS
    java系列视频教程下载
    关于Mysql DATE_FORMAT() 日期格式
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/13270648.html
Copyright © 2011-2022 走看看