LCS Maximum Common subsequence. memset function requires include <string.h>
Time limit of MS
Memory Limit 65536 KB
Code length limit 16000 B
Procedures for the award of questions StandardAuthor Chen, Yue
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 this a normal human eye can distinguish on less than-different colors, so Eva's favorite colors is Limi Ted. 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 the 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
Source: >
#include<iostream>
#include<stdio.h>
#pragma warning(disable:4996)
using namespace std;
int like[201], given[10001], mp[201][10001];
int main(void) {
memset(mp, 0, sizeof(mp));
//freopen("Text.txt", "r", stdin);
int n;
cin >> n;
int m;
cin >> m;
for (int i = 1; i <= m; i++) {
cin >> like[i];
}
int p;
cin >> p;
for (int i = 1; i <= p; i++) {
cin >> given[i];
}
for (int i = 1; i <= m; i++) {
Span class= "KWD" >for ( int J = 1 J <= p J ++) {
Span class= "Typ" >int max = MP Span class= "pun" >[ i - 1 ][ j - 1 ;
if (mp[i - 1][j]>max)
max = mp[i - 1][j];
if (mp[i][j - 1]>max)
max = mp[i][j - 1];
if (given[j] == like[i])
max += 1;
mp[i][j] = max;
}
}
cout << mp[m][p];
return 0;
}
From for notes (Wiz)
1045. Favorite Color Stripe ()