• PAT 1045. Favorite Color Stripe (30)


    1045. Favorite Color Stripe (30)

    时间限制
    200 ms
    内存限制
    65536 kB
    代码长度限制
    16000 B
    判题程序
    Standard
    作者
    CHEN, Yue

    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动态规划,根据favorite数列,依次求得stripe最大favorite长度,每一层的最大长度又是通过叠加以及取最值来实现的。例如:

                           2  2  4  1  5  5  6  3  1  1  5  6

    2                 0  1  2  2  2  2  2  2  2  2  2  2  2
    2 3              0  1  2  2  2  2  2  2  3  3  3  3  3
    2 3 1           0  1  2  2  3  3  3  3  3  4  5  5  5
    2 3 1 5        0  1  2  2  3  4  5  5  5  5  5  6  6
    2 3 1 5 6     0  1  2  2  3  4  5  6  6  6  6  6  7

    通过代码实现即,判断favorite值与stripe中的值是否相等,不相等则取max(前一个值,遍历上一个favorite值时产生的当前值),即max(dp[j-1],dp[j]),相等则取前一个值+1;

    我最初想的相等取max(前一个值+1,遍历上一个favorite值时产生的当前值),即max(dp[j-1]+1,dp[j]),后来发现没有必要。这里需要证明dp[j-1]+1>dp[j],即dp[j-1]>=dp[j]。由于条件是favorite值与stripe[j]相等,可知,在之前的favorite值遍历中,不存在相等的情况,只有不相等,这时又要考虑不相等时max(dp[j-1],dp[j]),什么情况取dp[j-1],什么情况取dp[j]呢?

    经过探究发现,只有在之前的favorite值遍历中,有相等情况,dp[j]变大,当现在要去覆盖它时,就要比较dp[j-1]和dp[j]谁大;反过来说只要在之前的favorite遍历中,不存在相等情况,那么这里不相等条件下的dp[j]就等于dp[j-1];当前遇到相等条件下,也说明了之前没有遇到过相等条件,所以此时的dp[j]一定等于dp[j-1],证得dp[j-1]+1>dp[j]。

    #include <bits/stdc++.h>
    
    using namespace std;
    
    int N, M, K;
    int favorite[202];
    int stripe[10004];
    int dp[10004];
    
    int main()
    {
        cin>> N>> M;
        for(int i = 0; i < M; i++) {
            cin>> favorite[i];
        }
        cin>> K;
        for(int i = 0; i < K; i++) {
            scanf("%d", &stripe[i]);
        }
        for(int i = 0; i < M; i++) {
            for(int j = 0; j < K; j++) {
                //cout<< stripe[j]<< " "<< favorite[i]<< " ";
                if(stripe[j] == favorite[i]) {
                    //dp[j] = max(dp[j], dp[j-1]+1);
                    dp[j] = dp[j-1]+1;
                } else {
                    dp[j] = max(dp[j], dp[j-1]);
                }
                //cout<< dp[j]<< endl;
            }
        }
        cout<< dp[K-1];
        return 0;
    }
  • 相关阅读:
    springMVC 错误页面配置
    设计模式 11 —— 代理模式
    JAVA RMI 实例
    设计模式 10 —— 状态模式
    设计模式 9 —— 模板方法模式
    Java EE : 三、图解Session(会话)
    Java EE : 二、图解 Cookie(小甜饼)
    Java EE : 一、图解Http协议
    《JAVA与模式》之单例模式
    Java中如何克隆集合——ArrayList和HashSet深拷贝
  • 原文地址:https://www.cnblogs.com/ACMessi/p/8438620.html
Copyright © 2020-2023  润新知