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 (≤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.

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

解题思路:

题目大概意思是给出一串人物喜欢的颜色序列(用编号表示,颜色不重复),然后给出一串原始的颜色序列,除去其中不喜欢的颜色,在剩下的序列之中找出符合人物喜欢颜色序列排序的最长子序列(不要求连续,可重复,可遗漏)。

法一:

看到题目最开始想到的是递归的方法,对于已去除不喜欢颜色的序列K1,读取第一个元素

  1. 如果颜色是喜欢颜色序列K2的第一个颜色,那么继续往下读取。
  2. 如果K1[0]不等于K2[0],那么有两种可能,一是舍弃K1[0],继续往下读取。二是保留K1[0],将K2中K1[0]之前的元素都删除,继续往下读取,此时返回两种可能的最大值。

代码一:

66

#include <iostream> 
using namespace std;
int find(int sp,int tp,int cnt,int minposition,int lastcolor);
int order[10000],q[200];//排除不喜欢颜色剩下的子序列,喜欢队列及其所在位置 
int main(){
	int N,M,L;
	cin>>N>>M;
	int i,lastcolor;
	for(i=0;i<N;i++) 
		q[i]=-1;
	for(i=0;i<M;i++){
		int color;
		scanf("%d",&color);
		q[color-1]=i;
		if(i==0)	
			lastcolor=color-1;	
	}	
	cin>>L; 
	i=0;
	while(L--){
		int color;
		scanf("%d",&color);
		if(q[color]!=-1){
			order[i]=color-1;
			i++;
		}	
	}
	int length=find(0,i-1,0,0,lastcolor);
	printf("%d\n",length);
	return 0;
}
int find(int sp,int tp,int cnt,int minposition,int lastcolor){
	if(sp>tp)
		return cnt;
	while(q[order[sp]]<minposition){
		sp++;
		if(sp>tp)
			return cnt;	
	}
	if(order[sp]==lastcolor)
		return find(sp+1,tp,cnt+1,minposition,lastcolor);
	else {
		int a=find(sp+1,tp,cnt,minposition,lastcolor);
		lastcolor=order[sp];
		int position=q[lastcolor];
		int b=find(sp+1,tp,cnt+1,position,lastcolor);
		return max(a,b);		
	}	
}

法二:

参考了其他博主的解题思路之后,采用动态规划法求解,以减低运行时间,基本思路是首先给喜欢序列K1编号,从0至K-1,代表喜欢颜色在序列之中的位置。然后在原始颜色序列K2中除去不喜欢颜色,得到新序列K3,并构造一个新的数组K4,标号为颜色在K3序列中的位置,元素为对应颜色在K1中的位置。最后,原问题转化为在新序列K3中找到最长的非降序子序列。

构造数组dp,dp[i]表示K4从0到i的子序列中包含元素K4[i]的非递减最长子序列的长度,对于dp[i+1],其值满足:

对于dp=Max(dp[j]),其中0\leqslant j\leq iK4[j]\leq K4[i+1]dp[i+1]=Max(1,dp)

最后输出最大的dp[i]

代码二:

1

#include <iostream>
using namespace std;
int main(){
	int position[200],order[10000];
	int N,K,L,cnt,i;
	cin>>N>>K;
	for(i=0;i<N;i++)
		position[i]=-1;
	for(i=0;i<K;i++){
		int color;
		scanf("%d",&color);
		position[color-1]=i;	
	}
	cnt=0;
	cin>>L;
	while(L--){
		int color;
		scanf("%d",&color);
		if(position[color-1]>-1)
			order[cnt++]=position[color-1];	
	}	
	int ans=0,dp[10000],j;
	for(i=0;i<cnt;i++){
		dp[i]=1;
		for(j=0;j<i;j++)
			if(order[j]<=order[i])
				dp[i]=max(dp[i],dp[j]+1);
		ans=max(ans,dp[i]);	
	}
	printf("%d\n",ans);	
}

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值