PAT 甲 1045 Favorite Color Stripe

Eva希望从给定的颜色条纹中裁剪出她最喜欢的颜色,保持顺序,求剩余最喜欢颜色条纹的最大长度。这是一个涉及动态规划的问题,通过设置优先级数组,使用DP方法解决。当给定序列中没有Eva喜欢的颜色时,答案为0。
摘要由CSDN通过智能技术生成

原作者: edwardwjz

原文链接  http://blog.csdn.net/edwardwjz/article/details/79026173


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

题目意思:

将一个序列中的所有颜色,提取出某些使得这个序列按照 Eva 喜欢的颜色顺序排列,则满足这个情况的最长序列是多长。

用输入举例子。 6表示序列中有6种颜色。 第二行5表示 接下来有5种颜色是 Eva 喜欢的颜色,按顺序是 2 3 1 5 6.  第三行12表示给定的序列总长度为 12,接下来就是12个数字表示有12个这些颜色的序列


思路:

实际上就是求一个序列中满足某个关系的最长序列长度。题目是一个隐含的动态规划问题 。这是一个 DP 问题。经典问题是 一个数字序列中按原序列提取一些数,使得满足非递减情况的序列最长有多长。

可以看到,经典的非递减序列问题中,条件非递减就是自然数的条件,而这个题,Eva 喜欢的颜色顺序就是条件。所以设置一个数组,使得给喜欢的数字序列中的数字不同的优先级,则在给定的序列中求时,就可以用 DP 思路解决了


代码:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
// PAT 甲 1045 Favorite Color Stripe

int priority[220]={0};
int num[10086]={0};
int dp[10086] = {0};  // dp[i]表示从第1个元素到序列第 i+1 个元素满足条件的是有多长

int main(){
    int n,m;
    cin >> n; // 多少种颜色
    cin >> m;
    int temp;
    for (int i = 0; i < m; ++i){
        cin >> temp;
        priority[temp] = i+1; // 表示优先级 2 3 1 5 6 即 2的优先级是1,最高
    }
    cin >> m;
    int index = 0;
    for (int i = 0; i < m; ++i){
        cin >> temp;
        if (priority[temp] == 0)
            continue;
        num[index++] = temp;    // 得到除去没有优先级的序列
    }
    
//    if (index == 0){            // 不加这个,测试也通过,但是是不对的
//        cout << 0 << endl;
//        return 0;
//    }
//
    
    dp[0] = 1;
    for (int i = 1; i < index; ++i){    // 从第2个元素到最后一个元素,动态规划
        dp[i] = 1;                      // 如果不加,则测试点 2,4不通过
        for (int j = 0; j < i; ++j){
            if (priority[num[j]] <= priority[num[i]] && dp[j]+1>dp[i]){
                dp[i] = dp[j]+1;
            }
        }
    }
    
    // debug 输出
//    for (int i = 0; i < index; ++i)
//        cout << num[i] << " ";
//    cout << endl;
//    for (int i = 0; i < index; ++i)
//        cout << dp[i] << " ";
//    cout << endl;
    
    cout << *max_element(dp,dp+index) << endl;
    return 0;
}


总结:

1, 我测试后,发现这个题目 PAT 的测试用例中少一种情况,就是 给定的序列中没有 Eva 想要的数字,那么应该输出0。比如

7

3 1 2 3

2 6 7

这个测试用例是我自己写的,意思是总共有7种颜色, Eva喜欢的颜色是1 2 3, 而给定的颜色序列只有 6 7, 那么按照题意,输出长度,那么应该是0。 但这个用例其实官方是没有的这种情况的


2, for 循环中,我有一处加了个注释,说到了一点。  我一开始没有加,过了样例,但是测试点2,4不对。然后加了就过了,应该是某处不对,但是还没有思考明白

      dp[i] = 1;      //如果不加,则测试点 24不通过

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值