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 (≤104) 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
C++:
/*
@Date : 2018-08-27 22:44:05
@Author : 酸饺子 (changzheng300@foxmail.com)
@Link : https://github.com/SourDumplings
@Version : $Id$
*/
/*
https://pintia.cn/problem-sets/994805342720868352/problems/994805437411475456
*/
#include <iostream>
#include <cstdio>
using namespace std;
int N, M, L;
const int MAXL = 10005, MAXM = 205;
int l[MAXL], m[MAXM];
int maxL[MAXM][MAXL];
int main()
{
for (int i = 0; i != MAXM; ++i)
{
for (int j = 0; j != MAXL; ++j)
maxL[i][j] = 0;
}
scanf("%d %d", &N, &M);
for (int i = 1; i != M + 1; ++i)
scanf("%d", &m[i]);
scanf("%d", &L);
for (int i = 1; i != L + 1; ++i)
scanf("%d", &l[i]);
for (int i = 1; i != M + 1; ++i)
{
for (int j = 1; j != L + 1; ++j)
maxL[i][j] = maxL[i-1][j];
int c = m[i];
for (int j = 1; j != L + 1; ++j)
{
int temp = max(maxL[i-1][j], maxL[i][j-1]);
if (l[j] == c)
maxL[i][j] = temp + 1;
else
maxL[i][j] = temp;
}
}
printf("%d\n", maxL[M][L]);
return 0;
}
C:
/*
@Date : 2017-12-05 16:08:37
@Author : 酸饺子 (changzheng300@foxmail.com)
@Link : https://github.com/SourDumplings
@Version : $Id$
*/
/*
https://www.patest.cn/contests/pat-a-practise/1045
借助记忆矩阵, 例如对于题目中的例子:
0 2 2 4 1 5 5 6 3 1 1 5 6
0 0 0 0 0 0 0 0 0 0 0 0 0
2 2 0 1 2 2 2 2 2 2 2 2 2 2 2
2 3 3 0 1 2 2 2 2 2 2 3 3 3 3 3
2 3 1 1 0 1 2 2 3 3 3 3 3 4 5 5 5
2 3 1 5 5 0 1 2 2 3 4 5 5 5 5 5 6 6
2 3 1 5 6 6 0 1 2 2 3 4 5 6 6 6 6 6 7
可以发现,对于该矩阵每一行每一列的元素都是当前子列的长度最大值
并且每个元素都是其左边元素和上边元素的较大值,如果所在行列的对应的元素相等,则再加一
*/
#include <stdio.h>
#include <stdlib.h>
#define MAXN 201
#define MAXL 10001
int N;
int M;
int L;
int colors[MAXL];
int favorite[MAXN];
int maxlength = 0;
int result[MAXN][MAXL];
int Solve(void)
{
int i, j;
int max;
for (i = 0; i <= M; i++)
{
for (j = 0; j <= L; j++)
{
result[i][j] = 0;
}
}
for (i = 1; i <= M; i++)
{
for (j = 1; j <= L; j++)
{
max = result[i-1][j-1];
if (result[i-1][j] > max)
{
max = result[i-1][j];
}
if (result[i][j-1] > max)
{
max = result[i][j-1];
}
if (favorite[i] == colors[j])
{
result[i][j] = max + 1;
}
else
{
result[i][j] = max;
}
// int k;
// printf("$$max = %d\n", max);
// for (k = 1; k < L; k++)
// {
// printf("%d ", result[i][k]);
// }
// printf("$$\n");
}
}
return result[M][L];
}
int main()
{
scanf("%d", &N);
scanf("%d", &M);
int i;
for (i = 1; i <= M; i++)
{
scanf("%d", &favorite[i]);
}
scanf("%d", &L);
for (i = 1; i <= L; i++)
{
scanf("%d", &colors[i]);
}
maxlength = Solve();
// int j;
// for (i = 1; i <= M; i++)
// {
// for (j = 1; j <= L; j++)
// {
// printf("%d ", result[i][j]);
// }
// putchar('\n');
// }
printf("%d\n", maxlength);
return 0;
}