解法一:动态规划
按照喜欢的颜色的顺序定义优先级,剔除不喜欢的颜色后,剩下的数列就是一个最长不下降子序列问题。
#include <bits/stdc++.h>
const int MAX_N = 10001;
const int MAX_M = 201;
using namespace std;
int dp[MAX_N];
int colors[MAX_N];
// priority[i] 表示颜色i的优先级
int priority[MAX_M];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int N, M, L, tmp;
cin >> N;
cin >> M;
for (int i = 1; i <= M; ++i) {
cin >> tmp;
priority[tmp] = i;
}
cin >> L;
int len = 0;
for (int i = 0; i < L; ++i) {
cin >> tmp;
if (priority[tmp] != 0) {
colors[len++] = priority[tmp];
}
}
int ans;
// 最长不下降子序列
for (int i = 0; i < len; ++i) {
dp[i] = 1;
for (int j = 0; j < i; ++j) {
if (colors[i] >= colors[j]) {
dp[i] = max(dp[i], dp[j] + 1);
}
}
ans = max(ans, dp[i]);
}
cout << ans << "\n";
}