PAT(A)1045 Favorite Color Stripe (30分)

在这里插入图片描述

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;
}

发布了161 篇原创文章 · 获赞 7 · 访问量 7090

猜你喜欢

转载自blog.csdn.net/weixin_43778744/article/details/104083846