PAT甲级 1045 Favorite Color Stripe(30) (线性DP)

题目

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}.

输入

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 (≤10^{4}) 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.

输出

For each test case, simply print in a line the maximum length of Eva's favorite stripe.

样例输入 

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

样例输出 

7

题意理解

先输入一个数 表示颜色的总数 颜色从1到nu 共有nu种颜色

然后输入n 然后输入n个数 这是Eva 按照自己喜欢的颜色按照自己喜欢的顺序 排列的一个顺序

然后给你一条布条 长度最大为1e4的长度 要在这条布条里面最长的条纹长度

也就是要去掉Eva不喜欢的颜色 而且必须要根据Eva喜欢的顺序来的最大长度

那么问题其实就转换成了一个最长公共子序列的问题

集合表示:f[i][j] 表示a的前i个数字 和 b的前j个数字的最长公共子序列长度

集合划分:以a[i] b[j]是否在子序列中作为依据划分为以下几类

前三类都是建立在   a[i]不等于b[j]这个前提条件下 

  1. a[i]不在b[j]不在 那么直接从f[i-1][j-1]转移过来 而且在实际求解的情况的时候,这种情况已经包含在了2 和 3 这两种情况里了
  2. a[i]不在b[j]在 虽然看似求得是max=f[i-1][j] ,但是实际上无法用max=f[i-1][j]完全表示 因为就是求的是a的前i-1个数字中出现,并且我们在b的序列的前j个数字中出现,但此时b[j]并不一定出现在前面,但此时的条件是 a[i]不在b[j]在,但是我们最后求的是一个max,所以我们可以假设已经存在保证求重复子集的时候不把情况漏掉
  3.  a[i]b[j]不在  max=f[i][j-1]那么分析也是同2

  4.  a[i]等于b[j] 那么f[i][j-1]+1 也就是b[j]的前面一位增加1 和我当前f[i][j]取一个max即可 

代码 

#include<bits/stdc++.h>
using namespace std;
const int N=210;
const int M=10010;
int a[N],b[M];
int nu,n,m;
int f[N][M];
int main(){
    scanf("%d%d",&nu,&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    scanf("%d",&m);
    for(int i=1;i<=m;i++){
        scanf("%d",&b[i]);
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            f[i][j]=max(f[i][j-1],f[i-1][j]);
            if(a[i]==b[j]){
                f[i][j]=max(f[i][j],f[i][j-1]+1);
            }
        }
    }
    
    printf("%d\n",f[n][m]);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值