甲级PAT 1045 Favorite Color Stripe (30 分)(散列、转化为动态规划中的最长不下降子序列)

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

题目大意:

 一共有N种颜色,标号1-N,Eva喜欢一种将M种颜色按顺序排序的条纹色。现在有一块由L块颜色排序的条纹布,然后需要将这块布尽可能多的裁剪拼接成Eva喜欢的排序序列。

解题思路:

这里有点向动态规划里的最长不下降子序列,但是这里需要找最长按顺序排序的颜色标号,标号并非按递增顺序,而是根据输入的时候的顺序。

所以这里需要把输入的喜欢颜色的顺序转化成递增顺序排序的1,2,3……N,也就是用一个整数数组a[i]表示颜色标号i的先后顺序

题目中的2 3 1 5 6 就可以变成 a[2] = 1,a[3] = 2, a[1] = 3,a[5] = 4, a[6] = 5

然后就可以将下面L中颜色转化成喜欢颜色的顺序。用一个vector存储。

题目中的2 2 4 1 5 5 6 3 1 1 5 6变成了颜色序列对应的1 1 0 3 4 4 5 2 3 3 4 5

对于a[i] == 0的颜色,说明这个颜色并非Eva喜欢的颜色。所以不用存储到vector中。

vector实际存储的值为1 1 3 4 4 5 2 3 3 4 5

最后按照最长上升子序列对vector中的颜色顺序进行处理。到这里应该很多人就懂了。不懂最长不下降子序列怎么写的可以去百度一下经典的最长上升子序列。

注意:

用于动态规划的数组dp要开到10^4否则测试点3和4会报错

完整代码:

#include<iostream>
#include<vector>
using namespace std;
vector<int> color;
vector<int> v;
vector<int> dp(10010,1);

int main(){
	int N,M,L;
	int i,j,c,index,num,maxlen;
	cin >> N >> M;
	color.resize(N+1);
	for(i = 1; i <= M; i++){
		cin >> c;
		color[c] = i; 
	}
	cin >> L;
	for(i = 0; i < L; i++){
		cin >> c;
		if(color[c] != 0){
			v.push_back(color[c]);
		}
	}
	num = v.size();
	for(i = 0; i < num; i++){
		for(j = 0; j < i; j++){
			if(v[i] >= v[j]){
				dp[i] = max(dp[i],dp[j] + 1);
			}
		}
		if(dp[i] > maxlen) maxlen = dp[i]; 
	}
	cout<<maxlen;
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值