1045 Favorite Color Stripe (30 分) 递推 动规dp

我是研究僧i 提交于 2019-11-28 11:07:42

我是hxx,很没写博客了,废话不多说,看题.

 

1045 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

 题目大意:

题目的意思,给你两个数列,让你按照第一个数列的顺序求出第二个数列的最大连续长度.

有那么点最长子串或是最长上升子序列的意思,所以就想到了动规!

 

解题思路:

  以第一个数组为order遍历第二个数组,当你遇见与当前值相等的第二序列值的时候 cc++,如果不相等 但是在第一个序列后半段出现了,则他的值为max(dp[j],cc+1); 就是这样一个表达式.cc只有在遇见和第一序列此前值相等的值的情况下才会自增

 

参考代码:

#include <iostream>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
vector<int> data;
vector<int> fav;
map<int,int> mp1;
vector<int> dp;
int main(){
    int n;

    cin >> n;
    int m;
    cin >> m;
    for(int i= 0; i < m ; ++i){
        int v;
        cin >> v;
        mp1[v] = 1;
        fav.push_back(v);
    }

    cin >> m;
    dp.resize(m + 1);
    for(int i = 0 ; i < m ; ++i){
        int v;
        cin >> v;
        if(mp1[v] == 1)
            data.push_back(v);
    }


    int ll = 0;
    int maxv = 0;

    bool flag = true;
    for(int i = 0 ; i < fav.size() ; i++){
        int cc = 0;

        for(int j = ll ; j < data.size() ;j++){
            if(i == 0 && data[j] == fav[i] && flag){
                ll = j;
                flag =false;
            }
            if(data[j] == fav[i]){
                dp[j] = max(dp[j],cc+1);
                cc++;
                if(dp[j] > cc){
                    cc = dp[j];
                }
            }else if(find(fav.begin() + i + 1,fav.end(),data[j]) != fav.end()){
                dp[j] = max(cc+1,dp[j]);
            }
            maxv = max(dp[j],maxv);

        }

    }

    cout << maxv << endl;
    return 0;
}

 

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!