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 her ...
- 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. ...
随机推荐
- Fiddler抓包9-保存会话(save)【转载】
本篇转自博客:上海-悠悠 原文地址:http://www.cnblogs.com/yoyoketang/tag/fiddler/ 前言 为什么要保存会话呢?举个很简单的场景,你在上海测试某个功能接口的 ...
- MY97 日期控件只输入今天之前的值
<script type="text/javascript"> function GetDate() { ...
- MVC5的坑
事情是这样的,今天在写一个功能模块的时候,创建的方法,到controller里,死活为null 以前从没出现这种情况啊,但是区别是这个代码是多层跳转进来的,难道是页面跳转太多,还记得之前的model, ...
- 腾讯微信支付,程序员是如何让jQuery代码付钱的
微信支付和支付宝支付已经是我们生活中不可确实的两个金融软件了,也是必备的,小编认为小钱用微信,大钱用支付宝. 下面这个图是我们生活中用腾讯微信支付平台的最后一个页面,大家想不想知道这个页面是如果做出来 ...
- 动态规划-最长上升子序列(LIS模板)多解+变形
问题描述 一个数的序列bi,当b1 < b2 < ... < bS的时候,我们称这个序列是上升的.对于给定的一个序列(a1, a2, ..., aN),我们可以得到一些上升的子序列( ...
- 最小生成树 (Minimum Spanning Tree,MST) --- Kruskal算法
本文链接:http://www.cnblogs.com/Ash-ly/p/5409265.html 引导问题: 假设要在N个城市之间建立通信联络网,则连通N个城市只需要N - 1条线路.这时,自然会考 ...
- 洛谷——P2026 求一次函数解析式
P2026 求一次函数解析式 题目背景 做数学寒假作业的怨念…… 题目描述 给定两个整点的坐标,求它们所在直线的函数解析式(一次函数). 输入输出格式 输入格式: 输入共两行. 第一行有两个整数x1, ...
- PDF审计工具peepdf
PDF审计工具peepdf PDF是Portable Document Format(便携式文档格式)的缩写.它是Adobe公司推出的文件格式规范.现在,PDF是网络电子书籍的主流格式.由于PDF ...
- [Contest20180418]物理竞赛
题意:在一个三维空间中有一个轴,轴上有一个垂直于轴的半径为$R$的凸透镜(光心在轴上)和$n$个点光源,假设每个点光源发出的$1$单位光都刚好覆盖凸透镜,现在有一个半径为$r$的圆形光屏,问光屏最多能 ...
- http 头信息详解(转)
HTTP(HyperTextTransferProtocol)是超文本传输协议的缩写,它用于传送WWW方式的数据,关于HTTP协议的详细内容请参考RFC2616.HTTP协议采用了请求/响应模型.客户 ...