A1045 Favorite Color Stripe (30 分)

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 (≤10​4​​) 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

题意:

给出m种颜色作为主人公Eva喜欢的颜色(同时也是喜欢的顺序),后给出一串长度为L的颜色序列。现在要去掉Eva不喜欢的颜色,然后求剩余序列的一个子序列,使得这个子序列满足喜欢的顺序(不一定输出所有颜色),且为满足条件的子序列中长度最长的子序列。输出其长度。

第一行:N 表示有多少种颜色

第二行:表示喜欢m = 5种颜色,分别为2 3 1 5 6

第三行:长度为L = 12 的序列

思路:

对于喜欢颜色的序列,可以做一个映射,映射到递增序列,这样问题就转化为最长不下降子序列LIS的问题(要注意:选出的子序列不一定是连续的,例如{1,2,3,-1,-2,7,9},最长不下降子序列为{1,2,3,7,9})

注意:

  1. 颜色种类最多200,序列长度最多10000,要注意数组大小
  2. LIS的时间复杂度为O(L^2),这样下来10 ^8 数量级比较大,但是LIS中数组连续操作,cache命中率较高,不会超时
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxc = 210;       //最大颜色数
const int maxn = 10010;     //最大L
int HashTable[maxc];
int A[maxn], dp[maxn];
int main(){
    int n, m, x;
    scanf("%d%d", &n, &m);  //其实n不用
    memset(HashTable, -1, sizeof(HashTable));   //初始化为-1,表示不喜欢
    for(int i = 0; i < m; i++){
        scanf("%d", &x);
        HashTable[x] = i;       //映射
    }
    int L, num = 0;             //num存放喜欢颜色的总数
    scanf("%d", &L);
    for(int i = 0; i < L; i++){
        scanf("%d", &x);
        if(HashTable[x] >= 0){      //只将喜欢颜色对应的映射数字存入数组a
            A[num++] = HashTable[x];
        }
    }
    //以下为LIS问题的模板
    int ans = -1;       //记录最大的dp[i]
    for(int i = 0; i < num; i++){   //按顺序计算dp[i]的值
        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;      //状态转移方程,用来更新dp[i]
            }
        }
        ans = max(ans, dp[i]);
    }
    printf("%d\n", ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_35093872/article/details/87981813