1045 Favorite Color Stripe (30 分) 遞推 動規dp

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

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章