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 (<=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
//最长不下降子序列
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = ;
const int maxc = ;
int hashTable[maxn];
int A[maxc],dp[maxc]; //数组A和dp的大小和L有关
int main(){
int n,m,x;
memset(hashTable,-,sizeof(hashTable));
scanf("%d%d",&n,&m);
for(int i = ; i < m; i++){
scanf("%d",&x);
hashTable[x] = i;
}
int L,num = ;
scanf("%d",&L);
for(int i = ; i < L; i++){
scanf("%d",&x);
if(hashTable[x] >= ){
A[num++] = hashTable[x];
}
}
int ans = -;
for(int i = ; i < num; i++){
dp[i] = ;
for(int j = ; j < i; j++){
if(A[i] >= A[j] && dp[i] < dp[j] + ){
dp[i] = dp[j] + ;
}
}
ans = max(ans,dp[i]);
}
printf("%d",ans);
return ;
}
//最长公共子序列
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = ;
const int maxL = ;
int dp[maxL][maxL],A[maxn],B[maxL];
int main(){
int n,m,x;
scanf("%d%d",&n,&m);
for(int i = ; i <= m; i++){ //[1,n]
scanf("%d",&A[i]);
}
int L;
scanf("%d",&L);
for(int i = ; i <= L; i++){ //[1,L]
scanf("%d",&B[i]);
}
for(int i = ; i <= m; i++){ //[0,m]
dp[i][] = ;
}
for(int i = ; i <= L; i++){ //[0,L] 四个取值范围存疑
dp[][i] = ;
}
for(int i = ; i <= m; i++){
for(int j = ; j <= L; j++){
int MAXV = max(dp[i-][j],dp[i][j-]);
if(A[i] == B[j]){
dp[i][j] = MAXV+;
}else{
dp[i][j] = MAXV;
}
}
}
printf("%d",dp[m][L]);
return ;
}
1045 Favorite Color Stripe (30)的更多相关文章
- 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) -LCS允许元素重复
题目如下: Eva is trying to make her own color stripe out of a given one. She would like to keep only her ...
- 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 ...
- 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分)(简单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. ...
随机推荐
- Java的基础类型笔记
数据类型 大小 byte(字节) 1(8位) shot(短整型) 2(16位) int(整型) 4(32位) long(长整型) 8(32位) f ...
- GitHub预览网页[2019最新]
GitHub预览网页 1. 创建仓库 2. 设置页面预览 3. 上传html 4. 访问网页 1. 创建仓库 登陆GitHub创建仓库 datamoko 添加基本信息: 仓库名.仓库描述,然后点击创建 ...
- kubernetes第十章--ConfigMap 管理配置
- 关于Eclipse导入maven项目报空指针异常
今天新建了一个maven项目,因为是通过公司的工具新建的,代码拉下来就有src.pom.xml文件. 导入Eclipse却报空指针异常.具体如下: An error has occurred. See ...
- awvs 中文手册详细版(含10.5及12版本)
目录: 0×00.什么是Acunetix Web Vulnarability Scanner ( What is AWVS?) 0×01.AWVS安装过程.主要文件介绍.界面简介.主要操作区域简介(I ...
- asp.net代码审计起始篇之系统搭建
最近开始学习asp.net的代码审计,在开始审计之前除了要对语言有些基本的了解,还需要会在本地搭建demo网站方便调试和复现漏洞 准备工作:操作系统:我用的是win10 数据库:我用的sql serv ...
- [AIR] NativeExtension在IOS下的开发实例 --- IOS项目的创建 (一)
来源:http://bbs.9ria.com/thread-102037-1-1.html 最近看到本版块的很多关于NativeExtension的应用.但是都是在Android下面的应用.也有很多朋 ...
- LAMP环境搭建之编译安装指南(php-5.3.27.tar.gz)
测试环境:CentOS release 6.5 (Final) 软件安装:httpd-2.2.27.tar.gz mysql-5.1.72.tar.gz php-5.3.27.tar.gz 1 ...
- EhLib使用全攻略
使用 TDBSumList 组件 还记得以前有朋友问过这样一个问题:在 DBGrid 下如何像 Excel 一样能够做统计计算,实话说,使用 DBGrid 来做的话着实不易,不过现在有了这个咚咚, ...
- 【hadoop】看懂WordCount例子
前言:今天刚开始看到map和reduce类里面的内容时,说实话一片迷茫,who are you?,最后实在没办法,上B站看别人的解说视频,再加上自己去网上查java的包的解释,终于把WordCount ...