强烈推荐,刷PTA的朋友都认识一下柳神–PTA解法大佬
本文由参考于柳神博客写成
还有就是非常非常有用的 算法笔记 全名是
算法笔记 上级训练实战指南 //这本都是PTA的题解
算法笔记
PS 今天也要加油鸭
题目原文
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 (≤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
Sample Output:
7
生词如下:
PS:基本上看懂了,但是看漏了一句,或者说,意思基本上猜对了.
那个喜欢的颜色不一定要出现没有注意到
题目大意:
给出m中颜色作为主人公Eva喜欢的颜色,同时也给出顺序,然后给出一串长度为L的颜色序列,现在要去掉这个序列Eva不喜欢的颜色,然后求剩余序列的一个子序列,使得这个子序列表示的颜色顺序符合Eva喜欢的颜色的顺序(不一定要求所有喜欢的颜色都出现),且为所有满足这个条件的子序列中长度最长的子序列,输出其长度.
思路如下:
我们可以吧读入的喜欢的颜色,映射一下.HashTable,然后就可以用LIS的算法来写了
代码如下:
#include<cstdio>
#include<string>
#include<algorithm>
using namespace std;
const int maxc = 210; //最大颜色数
const int maxn = 10010; //最大L
int HashTable[maxc]; //将喜欢的颜色序列颜色映射为递增序列,不喜欢的颜色映射为-1
int A[maxn], dp[maxn]; //最长不下降子序列的原数组A和DP数组
int main(void) {
int n, m, x;
scanf("%d%d", &n, &m); //其实n用不到
fill(HashTable, HashTable + maxc, -1);
for (int i = 0; i < m; ++i) {
scanf("%d", &x);
HashTable[x] = i; //将喜欢的颜色按顺序映射到递增序列 0,1 ....... m-1中
}
int L, num = 0; //num存放颜色序列中Eva喜欢的颜色的总数
scanf("%d", &L);
for (int i = 0; i < L; ++i) {
scanf("%d", &x);
if (HashTable[x] >= 0) A[num++] = HashTable[x];
}
//以下全部为LIS问题的模板
int ans = -1;
for (int i = 0; i < num; ++i) { //循环条件请不要写i<n或者i<L之类的
dp[i] = 1;
for (int j = 0; j < i; ++j) {
if (A[j] <= A[i] && dp[i] < dp[j] + 1) dp[i] = dp[j] + 1;
}
ans = max(ans, dp[i]);
}
printf("%d\n", ans);
return 0;
}
如果这篇文章对你有张帮助的话,可以用你高贵的小手给我点一个免费的赞吗
相信我,你也能变成光.
如果你有任何建议,或者是发现了我的错误,欢迎评论留言指出.