【PAT甲级A1045】Favorite Color Stripe (30分)(c++)

编程知识 行业动态 更新时间:2024-06-13 00:22:27

1045 Favorite Color Stripe (30分)

作者:CHEN, Yue
单位:浙江大学
代码长度限制:16 KB
时间限制:400 ms
内存限制:64 MB

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 (≤10​4) 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

题意:

根据所给序列规则,求最长不下降子列长度。

思路:

如例题,在满足2<3<1<5<6的前提下,求数列的最长不下降子列。那么只要将{2,3,1,5,6}记录下标,如下表分别为1,2,3,4,5,那么令arr[2]=1,arr[3]=2,...arr[6]=5,就能用来表示大小顺序了,之后根据最长不下降子列的方法使用动态规划dp[i]=max{1,dp[j]+1}(j=1,2,3...,i-1&&arr[A[j]]<arr[A[i]]).。

参考代码:

#include <cstdio>
#include <algorithm>
using namespace std;
const int INF=1e5;
int main()
{
    int n,m,l;
    scanf("%d%d",&n,&m);
    int arr[n+1],index;
    fill(arr,arr+n+1,INF);
    for(int i=1;i<=m;i++){
        scanf("%d",&index);
        arr[index]=i;
    }
    scanf("%d",&l);
    int line[l+1],dp[l+1];
    for(int i=1;i<=l;i++)scanf("%d",&line[i]);
    for(int i=1;i<=l;i++){
        dp[i]=1;
        for(int j=1;j<i;j++){
            if(arr[line[i]]>=arr[line[j]]&&dp[j]+1>dp[i]&&arr[line[i]]!=INF){
                dp[i]=dp[j]+1;
            }
        }
    }
    printf("%d\n",*max_element(dp+1,dp+l+1));
    return 0;
}

如有错误,欢迎指正

更多推荐

【PAT甲级A1045】Favorite Color Stripe (30分)(c++)

本文发布于:2023-04-02 12:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/44ec9dd2d91901b5b2aff950d9318c07.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Favorite   PAT   Stripe   Color

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!