Pattern matching of "PAT 1045" favorite Color stripe longest common subsequence lcs__ series

Source: Internet
Author: User

1045. Favorite Color Stripe ( time limit) ms
Memory Limit 32000 KB
Code length limit 16000 B
Standard author CHEN, Yue

Eva is trying to make her own color stripe out of a given one. She would like-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-different colors, so Eva ' s favorite colors are Limi Ted. However The original stripe could is very long, and Eva would like to have the remaining favorite and the stripe Length. So she needs your help-find her the best result.

Note that the solution might is unique, but you are 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}.

Input Specification:

Each input file contains one test case. For each case, the contains a positive integer N (<=200) which be the total number of colors involved (and H ence 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 fav Orite order. Finally the third line starts with a positive integer L (<=10000) which was the length of the given stripe, followed by L colors on the stripe. The numbers in a line a separated by a.

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
The following:

The topic is abstracted to find the longest common subsequence of two sequences, but the public part allows the elements to be repeated.

Analysis:

is a variant of the longest common subsequence (lcs,longest Common subsequence) -The public part can repeat elements.



Code:

#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <cstring>

using namespace std;

Before this code is used, you need to delete the following two lines + system ("PAUSE")
ifstream fin ("in.txt");
#define CIN fin

int like[201]={0};
int given[10001]={0};
int len[201][10001]={0};

int LCS (int row,int col)
{
	int i,j;
	int Max;
	for (i=1;i<=row;i++) {for
		(j=1;j<=col;j++) {
			max = len[i-1][j-1];
			if (Max < len[i][j-1]) max = len[i][j-1];
			if (Max < len[i-1][j]) max = len[i-1][j];		First find the maximum value if
			(Like[i]==given[j]) {				//if equal, the maximum value of +1
				len[i][j] = max+1 to the left, top, left top three values
			else{
				len[i][j] = max;
	}} return len[row][col];
}


int main ()
{
	int n,m,l;
	cin>>n>>m;
	int i;
	for (i=0;i<m;i++) cin>>like[i+1];
	cin>>l;
	for (i=0;i<l;i++) cin>>given[i+1];

	Cout<<lcs (m,l) <<endl;

	System ("PAUSE");
	return 0;
}

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.