PAT 甲级 1045 Favorite Color Stripe(DP)

题目链接 Favorite Color Stripe

题意:给定A序列和B序列,你需要在B序列中找出任意一个最长的子序列,使得这个子序列也是A的子序列

(这个子序列的相邻元素可以重复)

只要求出这个子序列长度的最大值即可

很基础的DP题,设f[i]为当前以a[i]结尾的子序列的长度的最大值,扫描B序列的每个元素时实时更新即可。

#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b)	for (int i(a); i <= (b); ++i)
#define dec(i, a, b)	for (int i(a); i >= (b); --i)

typedef long long LL;

const int L = 10010;
const int N = 210;

int a[L], c[N], f[N];
vector <int> v[N];
int n, m, l;
int ans;

int main(){

	scanf("%d%d", &n, &m);
	rep(i, 1, m){
		scanf("%d", c + i);
		v[c[i]].push_back(i);
	}

	scanf("%d", &l);
	rep(i, 1, l) scanf("%d", a + i);

	rep(i, 1, l){
		for (auto u : v[a[i]]){
			if (f[u]) ++f[u];
			rep(j, 0, u - 1) f[u] = max(f[u], f[j] + 1);

		}
	}

	rep(i, 1, m) ans = max(ans, f[i]);
	return 0 * printf("%d\n", ans);
}
时间: 2024-10-15 12:41:38

PAT 甲级 1045 Favorite Color Stripe(DP)的相关文章

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

codeforces219C - Color Stripe DP+贪心

题意:给你n,k,大意就是说给你一个已经涂满颜色长为n的字符串,现有k种颜色可以选择,问你最少要改变多少个箱子的颜色使得相邻箱子之间颜色不同. 解题思路:当k = 2 时单独讨论,不能用贪心,其余情况都可贪心得到. 解题代码: 1 // File Name: 219c.cpp 2 // Author: darkdream 3 // Created Time: 2014年07月26日 星期六 15时45分56秒 4 5 #include<vector> 6 #include<list>

【PAT甲级】1045 Favorite Color Stripe (30 分)(DP)

题意: 输入一个正整数N(<=200),代表颜色总数,接下来输入一个正整数M(<=200),代表喜爱的颜色数量,接着输入M个正整数表示喜爱颜色的编号(同一颜色不会出现两次),接下来输入一个正整数L(<=10000),代表条带的长度,接着输入L个正整数表示条带上的颜色的编号.输出以喜爱颜色顺序排列的最长子串长度(不必每种颜色都有,只保证相对位置相同,同种颜色可连续). 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>

PAT 1045. 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

1045. Favorite Color Stripe (30)

LCS 最大公共子序列. memset函数需要include <string.h> 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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 cut

1045 Favorite Color Stripe (最长不下降子序列 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

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 favorite colors in her favorite order by cutting off those unwanted pieces and sewing the remaining parts together to form her favorite color stripe.

pat1045. Favorite Color Stripe (30)

1045. Favorite Color Stripe (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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 t

PAT1045. 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