1045. Favorite Color Stripe (-lcs) allow element repetition

Source: Internet
Author: User
Tags repetition

The topics are as follows:

Eva is trying-make she own color stripe out of a given one. She would like to keep only She 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 on less than-different colors, so Eva ' s Favorite colors is limited. However the original stripe could is very long, and Eva would like to has the remaining favorite stripe with the maximum Length. So she needs your help to find her the best result.

Note that the solution might is not a unique, but is only has 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 is given in her favorite order as {2 3 1 5 6}, then she had 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 are the total number of colors involved (and H ence The colors is 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 fav Orite order. Finally the third line starts with a positive integer L (<=10000) which are the length of the given stripe, followed by L colors on the stripe. All the numbers in a line is 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:
65 2 3 1 5 612 2 2 4 1 5 5 6 3 1 1 5 6
Sample Output:
7


This topic, according to the normal thinking solution, should use the longest common sub-sequence algorithm LCS, but unlike the regular LCS, the regular LCS is from two sequences in index increment order, do not repeat the selection of the largest common child columns, and now the problem is in sequence B according to the order of the elements in a can be repeated to find the maximum child column, This is more abstract, for example, for a sequence:

a={2,3,1,5,6} b={2,2,4,1,5,5,6,3,1,1,5,6}

If it is a regular LCS, the child column we find will be {2,3,1,5,6} because B completely contains a (not necessarily contiguous)

If it is a repeatable LCS, we can choose {2,2,3,1,1,5,6}, which is the variant LCS.

For the general LCS(refer to the algorithm Introduction 390 page 15.4 for the LCS algorithm), only a[i] = B[j] When the current maximum length of the child column is maxlen[i-1][j-1]+1, other cases take maxlen[i-1][j] or maxlen[i][j-1] in the maximum value, such an algorithm can only not repeat to find the child column, if you want to consider repetition, you should modify the algorithm, regardless of circumstances, take maxlen[i-1][j-1], maxlen[i-1][j] and maxlen[i][j-1] The maximum value, if A[I]=B[J], is based on the maximum value of +1, so that you can handle the situation of duplication.

#include <iostream> #include <stdio.h> #include <stdlib.h> #include <vector>using namespace std    ; int maxlen[201][10001] = {0};int main () {int n,m,l;    Cin >> N;    Cin >> M;    Vector<int> like (m+1);    int num;        for (int i = 1; I <= M; i++) {scanf ("%d", &num);    Like[i] = num;    } cin >> L;    vector<int> seq (l+1);        for (int i = 1; I <= L; i++) {scanf ("%d", &num);    Seq[i] = num;    } int max = 0;            for (int m = 1, M <= m; m++) {for (int n = 1; n <= L; n++) {max = maxlen[m-1][n-1];            if (Max < maxlen[m-1][n]) max = Maxlen[m-1][n];            if (Max < maxlen[m][n-1]) max = maxlen[m][n-1];            if (like[m] = = Seq[n]) {Maxlen[m][n] = max + 1;            }else{maxlen[m][n] = max; }//if (like[m] = = Seq[n]) {//Maxlen[m][n] = maxlen[m-1][n-1] + 1;//}else if (maxlen[m -1][n] &Gt;= maxlen[m][n-1]) {//Maxlen[m][n] = maxlen[m-1][n];//}else{//maxlen[m][n] = m    axlen[m][n-1];//}}} cout << maxlen[m][l] << Endl; return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

1045. Favorite Color Stripe (-lcs) allow element repetition

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.