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 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允许元素重复的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 【PAT甲级】1045 Favorite Color Stripe (30 分)(DP)
题意: 输入一个正整数N(<=200),代表颜色总数,接下来输入一个正整数M(<=200),代表喜爱的颜色数量,接着输入M个正整数表示喜爱颜色的编号(同一颜色不会出现两次),接下来输入一个 ...
- PAT (Advanced Level) 1045. Favorite Color Stripe (30)
最长公共子序列变形. #include<iostream> #include<cstring> #include<cmath> #include<algori ...
- 1045 Favorite Color Stripe 动态规划
1045 Favorite Color Stripe 1045. Favorite Color Stripe (30)Eva is trying to make her own color strip ...
- 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. ...
随机推荐
- 初识 Runtime
前言 之前在看一些第三方源码的时候,时不时的能碰到一些关于运行时相关的代码.于是乎,就阅读了一些关于运行时的文章,感觉写的都不错,写此篇文章为了记录一下,同时也重新学习一遍. Runtime简介 Ru ...
- sololearn的c++学习记录_4m11d
Function Templates Functions and classes help to make programs easier to write, safer, and more main ...
- 设计模式之中介者模式(Mediator )
中介者模式是关于数据交互的设计模式,该模式的核心是一个中介者对象,负责协调一系列对象之间的不同的数据请求,这一系列对象成为同事类.如房产中介(简直不想提它),买房的卖房的,租房的放租的都到房产中介那里 ...
- ACM 人见人爱A^B
求A^B的最后三位数表示的整数. 说明:A^B的含义是"A的B次方" Input输入数据包含多个测试实例,每个实例占一行,由两个正整数A和B组成(1<=A,B<=10 ...
- webpack 1.x 配合npm scripts管理多站点
需求场景: 希望通过一个webpack文件夹管理多个站点的打包流程. 假设现在我要为站点domain配置打包流程. npm 添加淘宝镜像 你懂得 vim ~/.npmrc registry = htt ...
- 阿里云服务器云数据库免费体验(Java Web详细实例)
一.效果展示 博主部署了两个war包到阿里云服务器上,一个是没有连接数据库的,另外一个是连接了数据库的. (由于阿里云服务器免费使用15天,下面链接约2016年3月9日后无效) (1)无数据库版访问地 ...
- TCP发送源码学习(2)--tcp_write_xmit
一.tcp_write_xmit()将发送队列上的SBK发送出去,返回值为0表示发送成功.函数执行过程如下:1.检测拥塞窗口的大小.2.检测当前报文是否完全处在发送窗口内.3.检测报文是否使用nagl ...
- Android设置竖屏
Android设置竖屏(禁止旋转屏) 清单文件,Activity下添加属性 android:screenOrientation="portrait" 如下 <?xml ver ...
- Hadoop2动态调整Log级别-以datanode的heartbeat log为例
在Hadoop中,有些log信息在正常情况下是不打印出来的.比如datanode发送heartbeat的日志. 代码位于BPServiceActor#sendHeartBeat方法中,如下图: 由于默 ...
- Objective-C方法的实现
Objective-C的方法被两种数据类型描述:一个是选择子(SEL),它用来描述方法的名称;另一个是实现(IMP),它用来描述方法被调用时实际执行的代码(它们基本上只是C函数的指针). 类似于SEL ...