题目如下:

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

这道题目,按照正常的思路求解,应该使用最长公共子序列算法LCS,但与常规的LCS有所差别,常规LCS是从两个序列中按索引递增顺序,不重复的选取最大公共子列,而现在的问题是在序列B中按照A中的元素顺序可重复的找出最大子列,这样说起来比较抽象,下面举个例子,对于序列:

A={2,3,1,5,6} B={2,2,4,1,5,5,6,3,1,1,5,6}

如果是常规的LCS,我们找到的子列将会是{2,3,1,5,6},因为B完全的包含了A(不必连续)

如果是可重复的LCS,我们完全可以选择{2,2,3,1,1,5,6},这便是变种的LCS。

对于常规的LCS(关于LCS的算法请参考算法导论390页15.4节),只有A[i] = B[j]时才让当前的最大子列长度为maxLen[i-1][j-1]+1,其他情况则取maxLen[i-1][j]或者maxLen[i][j-1]中的最大值,这样的算法只能不重复的找出子列,如果要考虑重复,应该修改算法,无论什么情况,都取maxLen[i-1][j-1]、maxLen[i-1][j]和maxLen[i][j-1]中的最大值,如果A[i]=B[j],则在最大值的基础上+1,这样就可以处理重复的情况了。

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <vector> using namespace std; int maxLen[201][10001] = {0}; int main()
{
int N,M,L;
cin >> N;
cin >> M;
vector<int> like(M+1);
int num;
for(int i = 1; i <= M; i++){
scanf("%d",&num);
like[i] = num;
}
cin >> L;
vector<int> seq(L+1);
for(int i = 1; i <= L; i++){
scanf("%d",&num);
seq[i] = num;
}
int max = 0;
for(int m = 1; m <= M; m++){
for(int n = 1; n <= L; n++){
max = maxLen[m-1][n-1];
if(max < maxLen[m-1][n]) max = maxLen[m-1][n];
if(max < maxLen[m][n-1]) max = maxLen[m][n-1];
if(like[m] == seq[n]){
maxLen[m][n] = max + 1;
}else{
maxLen[m][n] = max;
}
// if(like[m] == seq[n]){
// maxLen[m][n] = maxLen[m-1][n-1] + 1;
// }else if(maxLen[m-1][n] >= maxLen[m][n-1]){
// maxLen[m][n] = maxLen[m-1][n];
// }else{
// maxLen[m][n] = maxLen[m][n-1];
// }
}
} cout << maxLen[M][L] << endl; return 0;
}

1045. Favorite Color Stripe (30) -LCS允许元素重复的更多相关文章

  1. 1045. Favorite Color Stripe (30) -LCS同意元素反复

    题目例如以下: Eva is trying to make her own color stripe out of a given one. She would like to keep only h ...

  2. PAT 甲级 1045 Favorite Color Stripe (30 分)(思维dp,最长有序子序列)

    1045 Favorite Color Stripe (30 分)   Eva is trying to make her own color stripe out of a given one. S ...

  3. 1045 Favorite Color Stripe (30)(30 分)

    Eva is trying to make her own color stripe out of a given one. She would like to keep only her favor ...

  4. 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 favor ...

  5. 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 favor ...

  6. 【PAT甲级】1045 Favorite Color Stripe (30 分)(DP)

    题意: 输入一个正整数N(<=200),代表颜色总数,接下来输入一个正整数M(<=200),代表喜爱的颜色数量,接着输入M个正整数表示喜爱颜色的编号(同一颜色不会出现两次),接下来输入一个 ...

  7. PAT (Advanced Level) 1045. Favorite Color Stripe (30)

    最长公共子序列变形. #include<iostream> #include<cstring> #include<cmath> #include<algori ...

  8. 1045 Favorite Color Stripe 动态规划

    1045 Favorite Color Stripe 1045. Favorite Color Stripe (30)Eva is trying to make her own color strip ...

  9. PAT 1045 Favorite Color Stripe[dp][难]

    1045 Favorite Color Stripe (30)(30 分) Eva is trying to make her own color stripe out of a given one. ...

随机推荐

  1. Python中模块json与pickle的功能介绍

    json & pickle & shelve 1. json的序列化与反序列化 json的使用需要导入该模块,一般使用import json即可. json的序列化 方法1:json. ...

  2. vue中的eventBus

    在vue2中,父子组件传递数据,父组件可以直接传递数据进子组件,而子组件通过调用父组件传递进来的方法,将自己的数据传递回去. 那兄弟组件之间,或者是兄弟组件的子组件之间如何传递呢? 当然vuex是一种 ...

  3. Redis出现多线程调用时抛出 [B cannot be cast to java.lang.Long] 异常

    原因分析: 多个线程同时调用了同一个jedis对象,导致内存数据被多个线程竞争,产生数据混乱 (或者大家都用通一个redis获取同一个实例,登录同一个账号使用缓存时报错) 解决方案:每个线程都new出 ...

  4. delphi 验证码识别(XE8源码)

    如题:源码下载

  5. 详解BLE 空中包格式—兼BLE Link layer协议解析

    BLE有几种空中包格式?常见的PDU命令有哪些?PDU和MTU的区别是什么?DLE又是什么?BLE怎么实现重传的?BLE ACK机制原理是什么?希望这篇文章能帮你回答以上问题. 虽然BLE空中包(pa ...

  6. Linux 虚存的性能问题

    虚存子系统是所有 UNIX 系统的核心组件.下面讨论虚存系统的实现及其对操作系统中几乎其他所有子系统的作用和影响.首先详细说明一些基本的内存管理问题:然后具体分析 Linux 操作系统如何实施虚存管理 ...

  7. UILabel 调整行间距

    /* 调整行间距 */ + (void)adjustLineSpacingOfLabel:(UILabel *)label to:(CGFloat)lineSpacing { NSString *te ...

  8. Android视频媒体相关,VideoView和开源框架vitamio

    虽然Android已经内置了VideoView组件和MediaPlayer类来支持开发视频播放器,但支持格式.性能等各方面都十分有限,但是Vitamio的确强大到没朋友! Vitamio 是一款 An ...

  9. rbac 概念

    1 权限管理 1.1 什么是权限管理 分享牛原创,分享牛系列.基本上涉及到用户参与的系统都要进行权限管理,权限管理属于系统安全的范畴,权限管理实现对用户访问系统的控制,按照安全规则或者安全策略控制用户 ...

  10. 微信小程序开发入门篇

    本文档将带你一步步创建完成一个微信小程序,并可以在手机上体验该小程序的实际效果. 开发准备工作 获取微信小程序的 AppID 登录 https://mp.weixin.qq.com ,就可以在网站的& ...