【PAT - 甲级1045】Favorite Color Stripe(30分)(dp,LIS类问题)

题干:

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

题目大意:

Eva试着从给出的布条中选出她自己的彩色布条。她想要仅仅保留按照她最喜欢的顺序排列的她喜欢的颜色,并且剪掉那些不喜欢的部分再将剩下的部分缝在一起来组成她最喜欢的彩色布条。所以她需要你的帮助来找出最好的结果。
注意结果可能不为1,但是你只需要告诉她最大的长度。举个例子,给你一个 {2 2 4 1 5 5 6 3 1 1 5 6}.的彩色布条。如果Eva最喜欢的顺序排列的最喜欢的颜色为{2 3 1 5 6},这样她有4种可能的最佳方案 {2 2 1 1 1 5 6}, {2 2 1 5 5 5 6}, {2 2 1 5 5 6 6}, 和{2 2 3 1 1 5 6}。(注意  最喜欢的顺序排列  的每个数字都不重复)

解题报告:

因为最喜欢的顺序排列的每个数字都不重复,也就是说我可以给这些数字重新定序。这样就转化为一个LIS问题了。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define FF first
#define SS second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
int n,m,len;
int a[222],b[MAX],dp[MAX],buf[MAX],top;
int bk[222];//颜色出现的最后一个位置 
int main()
{
	memset(bk,-1,sizeof bk);//init
	cin>>n >> m;
	for(int i = 1; i<=m; i++) cin>>a[i],bk[a[i]] = i;
	cin>>len;
	
	for(int x,i = 1; i<=len; i++) {
		scanf("%d",&x);
		if(bk[x] != -1) {
			b[++top] = bk[x];
		}
	}
	for(int i = 1; i<=top; i++) {
		dp[i] = 1;
		for(int j = 1; j<i; j++) {
			if(b[i] >= b[j]) dp[i] = max(dp[i],dp[j]+1);
		}
	}
	printf("%d\n",*max_element(dp+1,dp+top+1));
	return 0 ;
}

总结:

注意对于序列问题,可以用数字大小来表示前后关系。

还有另一种方法:看不太懂

https://blog.csdn.net/tjj1998/article/details/79951718

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#define INF 99999999
using namespace std;
int N,M,L;
int dp[210],v[210];
int main(){
    scanf("%d%d",&N,&M);
    int a;
    for(int i=0;i<M;i++){
        scanf("%d",&a);
        if(!v[a])v[a]=i+1;
    }
    scanf("%d",&L);
    for(int i=0;i<L;i++){
        scanf("%d",&a);
        if(v[a])
        for(int j=M;j>=v[a];j--){
            for(int k=v[a];k>=0;k--)
            dp[j]=max(dp[j],dp[k]+1);
        }
    }
    printf("%d\n",dp[M]);
    return 0;
}


发布了1106 篇原创文章 · 获赞 122 · 访问量 23万+

猜你喜欢

转载自blog.csdn.net/qq_41289920/article/details/104123404