即日起在codingBlog上分享您的技术经验即可获得积分,积分可兑换现金哦。

1045. Favorite Color Stripe (30)

编程语言 puppet_pyt 54℃ 0评论

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


DP问题,把每个喜欢的颜色映射顺序 再按顺序求最大子序列长度  例如 现在EVA喜欢的是 2 3 1 5 6 映射成1 2 3 4 5;然后每次遇到一个颜色将其映射成序列号

状态转移方程即maxlen[i] = max(maxlen[1], maxlen[2], …maxlen[i]) +1;

一开始测试点2一直错误,分析了很久以为是EVA喜欢的颜色顺序可能有重复的例如21212这样子,修改之后发现依旧不对,结果原来最大长度不一定是最后的颜色的最大长度。

从结果来看,没有喜欢颜色重复出现的测试数据,两种都AC了,不过从严谨性来说应该用第二种;

求状态转移方程上还可以有优化,因为如果maxlen[i]如果不是0 即已经存在maxlen[i] 一定是大于maxlen[i-1]的;所以只需要max(maxlen[i-1], maxlen[i])就可以了,

补充:感觉这个优化可能存在问题,虽然结果上也可以AC,不过有没有可能出现maxlen[i-1]不存在, maxlen[i-2]及以前存在的情况;


#include 
#include 
using namespace std;
int main()
{
    int co;
    int n, m, temp;
    int colors[205] = {0};
    int maxlencolor[205] = {0};
    vector L;
    scanf("%d", &co);
    scanf("%d", &n);
    for(int i=1; i<=n; i++)
    {
        scanf("%d", &temp);
        colors[temp] = i;
    }
    scanf("%d", &m);
    for(int i=0; i


可重复出现喜欢颜色


#include 
#include 
using namespace std;
int main()
{
    int co;
    int n, m, temp;
    int colors[205] = {0};
    int maxlencolor[205] = {0};
    bool exist[205] = {false};
    vector L;
    scanf("%d", &co);
    scanf("%d", &n);
    for(int i=1; i<=n; i++)
    {
        scanf("%d", &temp);
        colors[i] = temp;
        exist[temp] = true;
    }
    scanf("%d", &m);
    for(int i=0; i









转载请注明:CodingBlog » 1045. Favorite Color Stripe (30)

喜欢 (0)or分享 (0)
发表我的评论
取消评论

*

表情