1045 Favorite Color Stripe (30 分)
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
/**
本题题意:
给定n种颜色, 以及m种喜欢颜色的序号, 现在需要在l种喜欢的颜色序列中根据喜欢的颜色需要
顺序 需要筛选出最多数字序列
本题思路(可以用两种思路 LIS, LCS :
动态规划题目: (LIS最长不下降子序列 )
1.用a数组存放 m中喜欢颜色的序号, 下标存储颜色的需要i a[i]的值 为 此颜色的顺序序列
2.边界条件:
每个颜色代表的数字初始最大的序列都为1就是本身
时间复杂度 为 0(n2)
3.状态方程:
当 A[j] > A[i] && dp[j] < dp[i] + 1
dp[j] = dp[i] + 1;
**/
LIS:
#include<iostream>
#include<algorithm>
using namespace std;
int a[101], b[10005], dp[10005];
int main(){
int n, m, color, l;
scanf("%d%d", &n, &m);
for(int i = 1; i <= m; i++){
scanf("%d", &color);
a[color] = i;
}
scanf("%d", &l);
fill(dp, dp + l, 1); //边界条件
scanf("%d", &b[0]);
int maxnum = 1;
for(int i = 1; i < l; i++){
scanf("%d", &b[i]);
if(a[b[i]] == 0)
continue;
for(int j = 0; j < i; j++){
if(a[b[j]] != 0 && a[b[j]] <= a[b[i]] && dp[j] + 1 > dp[i]){
dp[i] = dp[j] + 1;
if(dp[i] > maxnum)
maxnum = dp[i];
}
}
}
printf("%d\n", maxnum);
return 0;
}
LCS:
/**
dp[i][j] (注意i对应的是行, j对应的是列)表示记录的是 s1串 的 i位置, s2串的 j位置 最大 的子串长度 (注意是最大)
因此 在 dp存储的最后一个元素将会是最大子串的长度
边界条件:
dp[i][0] dp[0][j] (注意i对应的是行, j对应的是列) 都为0 (因为 求数组时 0 - 1 会越界, 因此 设定数组下标1为第一个元素
因为本题求公共子串 可以有重复元素, 因此状态方程为, max(dp[i - 1][j], dp[i][j - 1]) + 1
**/
#include<iostream>
using namespace std;
int main(){
int n, m;
scanf("%d%d", &n, &m);
int a[n + 1];
for(int i = 1; i <= m; i++){
scanf("%d", &a[i]);
}
int l;
scanf("%d", &l);
int b[l + 1], dp[l + 1][n + 1]; //注意数组范围规定
for(int i = 1; i <= l; i++){
scanf("%d", &b[i]);
}
for(int i = 0; i <= n; i++)
dp[0][i] = 0;
for(int i = 0; i <= l; i++){
dp[i][0] = 0;
}
for(int i = 1; i <= l; i++){
for(int j = 1; j <= n; j++){
if(b[i] == a[j])
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) + 1;
else
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
}
printf("%d\n", dp[l][n]);
return 0;
}