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

题目大意:一个数字代表一个颜色,一串数字组成一个五颜六色的布匹。现在给一串数字,表示给出的布匹,同时给出一个Eva喜欢的颜色以及对应的顺序。要求通过对给出的布匹进行裁剪(删去数字)、缝合的方法,组成一段符合Eva喜欢的颜色而且尽量长的布匹。

思路:读取Eva喜欢的颜色及序列的同时,我们对颜色进行标号,标记的号码对应颜色在Eva喜欢的顺序中的位置,例如fav[i] = j,表示i颜色在Eva喜欢的顺序中的位置是j(下标从1开始)。读取给出的布匹颜色序列时,我们直接忽视Eva不喜欢的颜色,其余的存到数组中。最后动态规划(这里采用的是dp中的pull方法),对于一个颜色i(作为布匹的终点),若是在i之前找到一个颜色j,且满足:在Eva喜欢的颜色顺序中,j在i之前,那么dp[i] = max(dp[i], dp[j] + 1),然后更新一下最终答案maxsum。循环上述操作即可。

#include<iostream>
#include<algorithm>
using namespace std;
int n, m, l, fav[205], seq[10005], dp[10005];
int main()
{
    int index=0, maxsum=-1, tmp;
    cin >> n >> m;
    for(int i = 1; i <= m; ++i) {
        cin >> tmp;
        fav[tmp] = i;
    }
    cin >> l;
    for(int i = 0; i < l; ++i) {
        cin >> tmp;
        if(fav[tmp] >= 1)   seq[index++] = fav[tmp];
    }
    for(int i = 0; i < index; ++i) {
        dp[i] = 1;
        //动态规划的pull方法
        for(int j = 0; j < i; ++j) {
            if(seq[i] >= seq[j]) {
                dp[i] = max(dp[i], dp[j] + 1);
            }
        }
        maxsum = max(maxsum, dp[i]);
    }
    cout << maxsum << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值