zoukankan      html  css  js  c++  java
  • 1045 Favorite Color Stripe

    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 (≤) 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 (≤) followed by M Eva's favorite color numbers given in her favorite order. Finally the third line starts with a positive integer L (≤) 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

    题意:

      给出一组favorite color的序列,和一组colors stripe的序列,要求在color stripe序列中找出满足favorite color序列的子串,子串中元素可以重复,只要满足子串颜色的序列和喜欢颜色的序列相同即可。

    思路:

      这道题应该用DP来解决,dp[i][j] : 表示在喜欢颜色的序列中以i为下标的颜色结尾,在colors stripe中[:j]中满足喜欢颜色[:i]序列的子串长度。状态转移方程: dp[i][j] = max(dp[i-1])[j], dp[i]][j-1]]; dp[i-1][j]表在colors stripe中[:j]相等的情况下,favorite color的下表向后移动一位,可能是子串长度改变。 dp[i][j-1] : 表示在favorite color中[:i]相等的情况下color stripe下标向后移动一位可能使子串长度发生改变。

    Code:

     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 int main() {
     6     int n, m, l;
     7     cin >> n >> m;
     8     vector<int> colors(n + 1);
     9     set<int> found;
    10     for (int i = 1; i <= m; ++i) cin >> colors[i];
    11     cin >> l;
    12     vector<int> stripe(l + 1);
    13     for (int i = 1; i <= l; ++i) cin >> stripe[i];
    14     vector<vector<int> > dp(n + 1, vector<int>(l + 1, 0));
    15     for (int i = 1; i <= n; ++i) {
    16         for (int j = 1; j <= l; ++j) {
    17             dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    18             if (colors[i] == stripe[j]) dp[i][j]++;
    19         }
    20     }
    21     cout << dp[n][l] << endl;
    22     return 0;
    23 }
  • 相关阅读:
    作业 20181030-3互评Alpha版本
    Alpha阶段事后诸葛亮会议记录
    Alpha发布用户使用报告
    20181023-2 贡献分配
    作业 20181016-1 Alpha阶段贡献分配规则
    Scrum立会报告+燃尽图(十月三十日总第二十一次)
    OC中时间函数的使用
    OC中的集合详解
    面向对象的概念详解(转)
    集中类
  • 原文地址:https://www.cnblogs.com/h-hkai/p/12857924.html
Copyright © 2011-2022 走看看