PAT(A)1045 Favorite Color Stripe (30分)
程序员文章站
2022-07-08 23:02:44
...
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
思路:
把不符合要求的数去掉,然后按照给定的***作为下标。
代码
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
using namespace std;
#define endl '\n'
typedef long long ll;
int flag[201];
int a[10004];
int dp[10001];
int main()
{
int n, k;
cin >> n >> k;
for (int i = 1; i <= k; ++i)
{
int x;
cin >> x;
flag[x] = i;
}
int num = 0;
int m;
cin >> m;
for (int i = 0; i < m; ++i)
{
int x;
cin >> x;
if (flag[x] != 0)
a[num++] = flag[x];
}
int maxn = 0;
for (int i = 0; i < num; ++i)
{
dp[i] = 1;
for (int j = 0; j < i; ++j)
{
if (a[i] >= a[j])
dp[i] = max(dp[i], dp[j] + 1);
}
maxn = max(maxn, dp[i]);
}
cout << maxn << endl;
return 0;
}