【PAT A1045】Favorite Color Stripe

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 (≤ 1 0 4 10^4 104) 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

思路
今天总算发现做题的方法,其实刷题真的有点像小学的找规律题,如果给你一大串数字叫你找规律,你可能还真找不到,但从小规模的数开始,会发现规律其实并没有想象中的复杂。OK,我们来想想我们人在处理这种题的时候是怎么做的,先从小问题开始(以sample input为例)假设现在只给你一个数也就是2,你怎么做?第一步自然是去2 3 1 5 6里面去找有没有2对吧,发现有那么说明如果只有一个2,那么答案就是1,因为只有1个他想要的color;问题升级,现在有两个数2 2,现在怎么做?对于第二个2我们第一步还是去找2是否在她喜欢的color里面,接下来发现前面也是2,因此长度为2,到目前为止看起来是在说废话,那么接下来我们来考虑2 2 4 1 5 5 6 3 1 1 5 6的情况,假设我们当前已经走到了最后一个6,第一步仍然是去找6是否在她喜欢的颜色里面,发现是,那么我们要求的长度等于多少呢,既然6是在她喜欢的颜色序列排最后,那么说明我们要求的长度等于排除最后一个6之外的序列中要求的长度加1.那么如果最后一位的6是1呢,思考一下吧!(答案:分别以2,2 3,2 3 1为喜欢颜色序列的结果中的最大值+1)

#include <cstdio>
using namespace std;

int main(){
	int colors, M, len, stripe[210], input[10010];
	scanf("%d%d", &colors, &M);
	for(int i = 0; i < M; i++){
		scanf("%d", &stripe[i]);
	}
	scanf("%d", &len);

	//dp[i]用来表示当前位置以stripe数组前i为喜欢序列的结果
	int dp[210] = {0};
	for(int i = 0; i < len; i++){
		scanf("%d", &input[i]);
		int max = 0;
		for(int j = 0; j < M; j++){
			if(dp[j] > max)
				max = dp[j];
			if(input[i] == stripe[j]){
				dp[j] = max + 1;
				break;
			}
		}
	}

	int ans = 0;
	for(int i = 0; i < M; i++)
		ans = ans < dp[i] ? dp[i] : ans;
	printf("%d\n", ans);

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值