PAT 甲级 1045 Favorite Color Stripe (30分)

题目

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最喜欢的样式,同时要使得切掉的部分最少;
N表示总共有N种颜色,
给出Eva喜欢的M种颜色排序(有优先级的)条纹需要按照她喜欢的颜色顺序排列,可以有部分颜色没有,也可以有颜色重复;
只需要输出最大的剩余长度即可;

思路

记like[ ]为Eva喜欢的颜色数组,stripe[ ]为给出的L个条纹颜色数组
方法一
借鉴最长公共子序列的做法,只是这里多了颜色可以重复的条件,这样将Eva喜欢的颜色输入数组后,不需要考虑优先级的情况,因为就是按照优先级排列的;
d p [ i ] [ j ] = { m a x ( d p [ i − 1 ] [ j − 1 ] + 1 , d p [ i − 1 ] [ j ] + 1 ) s t t r i p e [ i − 1 ] = = l i k e [ j − 1 ] m a x ( d p [ i − 1 ] [ j ] , d p [ i ] [ j − 1 ] ) 否 则 dp[i][j] = \left\{ \begin{aligned} max(dp[i-1][j-1]+1,dp[i-1][j]+1) && sttripe[i-1]==like[j-1] \\ max(dp[i-1][j], dp[i][j-1]) &&否则 \end{aligned} \right. dp[i][j]={max(dp[i1][j1]+1,dp[i1][j]+1)max(dp[i1][j],dp[i][j1])sttripe[i1]==like[j1]
方法二
借鉴最长上升子序列方法,对于Eva喜欢的颜色作出不同颜色的优先级,即like[c] = i,数字越小,优先级越大;
遍历stripe数组,对每个颜色stripe[i-1],如果该颜色是Eva喜欢的颜色,则for循环遍历stripe起始位置到位置下标 i ,如果stripe[j-1]是Eva喜欢的颜色且其优先级比stripe[i-1]大或相等,则
d p [ i − 1 ] = m a x ( d p [ i − 1 ] , d p [ j − 1 ] + 1 ) dp[i-1] = max(dp[i-1], dp[j-1]+1) dp[i1]=max(dp[i1],dp[j1]+1),并更新maxlen,最后输出maxlen为最大长度

代码

// 方法一:LCS
#include<bits/stdc++.h>
using namespace std;
int main(int argc, const char * argv[]) {
    int N, M, L;
    cin>>N>>M;
    vector<int> like(M);
    for(int i=0; i<M; i++)
        cin>>like[i];
    cin>>L;
    vector<int> stripe(L);
    for(int i=0; i<L; i++)
        cin>>stripe[i];
    vector<vector<int>> dp(L+1, vector<int> (M+1));
    for(int i=0; i<=L; i++){
        for(int j=0; j<=M; j++){
            if(i==0 || j==0) dp[i][j] = 0;
            else{
                if(stripe[i-1] == like[j-1])
                    dp[i][j] = max(dp[i-1][j-1]+1, dp[i-1][j]+1);
                else
                    dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
            }
        }
    }
    cout<<dp[L][M];
    return 0;
}
//方法二 LIS
#include<bits/stdc++.h>
using namespace std;
int main(){
    int N, M, L, c, maxlen=0;
    cin>>N>>M;
    vector<int> like(205, 0);
    for(int i=1; i<=M; i++){
        cin>>c;
        like[c] = i;  // 用i表示优先级, 越小优先级越高
    }
    cin>>L;
    vector<int> stripe(L);
    for(int i=0; i<L; i++)
        cin>>stripe[i];
    vector<int> dp(L+1);
    dp[0] = 0;
    for(int i=1; i<=L; i++){
        if(like[stripe[i-1]] != 0){ //说明是喜欢的颜色
            dp[i] = 1;
            for(int j=1; j<i; j++){
                if(like[stripe[j-1]] != 0 && like[stripe[j-1]] <= like[stripe[i-1]])
                    dp[i] = max(dp[i], dp[j]+1);
            }
            maxlen = max(maxlen, dp[i]);
        }
    }
    cout<<maxlen;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值