Home PAT 1045 Favorite Color Stripe
Post
Cancel

PAT 1045 Favorite Color Stripe

Link

本质上是最长上升子序列的问题, 只是这里的“上升”是按题目给出的 order 来比较的.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <string.h>
#include <vector>
using namespace std;
int n,m,l,x,maxv;
int f[210],stripe[10010],dp[10010];
int main() {
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;++i){
        scanf("%d",&x);
        f[x]=i;
    }
    scanf("%d",&l);
    for(int i=1;i<=l;++i)
        scanf("%d",&stripe[i]);
    for(int i=1;i<=l;++i){
        if(!f[stripe[i]]) continue;
        dp[i]=1;
        for(int j=1;j<i;++j)
            if(f[stripe[j]]&&f[stripe[j]]<=f[stripe[i]])
                dp[i]=max(dp[i],dp[j]+1);
        maxv=max(maxv,dp[i]);
    }
    printf("%d\n",maxv);
    return 0;
}
This post is licensed under CC BY 4.0 by the author.