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}.
Eva在给定的颜色里条纹,她将以她喜欢的序列选择颜色,过滤自己不喜欢的,保留自己喜欢的纹路。据说在正常人的眼中可以区分200个人不同的颜色,因此Eva喜欢的颜色是有限的,无论如何,原生的条纹非常长,以及Eva喜欢保留最大的喜欢颜色长度,因此它需要你的帮助去寻找它的结果。注意解决方案不是唯一的,但是你只告诉他最长的长度,举个例子,给定一个颜色条纹,如果eva最喜欢的颜色序列是{2,3,1,5,6},然后她有四种可能最好的方案{2,2,1,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 (≤10 4 ) 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.
输入规格:每个输入文件包含一个测试样例,针对每个样例,第一包含正整数N,它是包含颜色的总数,颜色序号从1-N,第二行是M,M个Eva最喜欢颜色的序列,最后第三行L整数,代表颜色的长度,紧接着是L颜色长度,所有数字在一行由空格隔开。
Output Specification:
For each test case, simply print in a line the maximum length of Eva’s favorite stripe.
对于每个测试样例,仅仅打印最大的eva喜欢条纹的长度。
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核心思路
一个dp,一个rank,
完整代码#includeusing namespace std; int Rank[201] = {0}; int main() { int i,j,k; int N; cin >> N; int M,favorate[201]; cin >> M; for(i=1;i<=M;i++){ cin >> favorate[i]; Rank[favorate[i]] = i; } int L,old[10000]; cin >> L; for(i=0;i > old[i]; int longest_endwith[201] = {0}; for(i = 0;i max)max = longest_endwith[k]; } longest_endwith[j] = max+1; } int ans = 0; for(i=1;i<=M;i++){ if(ans 方法2
#include#include #include using namespace std; const int maxc = 210; const int maxn = 10010; int HashTable[maxc];//将喜欢的颜色序列映射为递增序列,不喜欢的颜色映射为-1 int A[maxn],dp[maxn];//最长1不下降子序列的原数组A和DP数组 int main(){ int n,m,x; scanf("%d%d",&n,&m); memset(HashTable,-1,sizeof(HashTable)); for(int i =0;i = 0){ A[num++] = HashTable[x]; } } int ans = -1; for(int i = 0;i 方法3
#include#include using namespace std; const int maxc = 210; //颜色的最大种类数 const int maxn = 10010; //颜色序列的最大长度 int A[maxc],B[maxn],dp[maxc][maxn]; int main(){ int n,m; scanf("%d%d",&n,&m); for(int i = 1;i<= m;i++){ scanf("%d",&A[i]);//读入序列A } int L; scanf("%d",&L); for(int i = 1;i<=L;i++){ scanf("%d",&B[i]);//读入序列B } //边界 for(int i =0;i<=n;i++){ dp[i][0] = 0; } for(int j =0;j<=L;j++){ dp[0][j] = 0; } //状态转移方程 for(int i =1;i<=m;i++){ for(int j = 1;j<=L;j++){ //取dp[i-1][j]、dp[i][j-1]的较大值 int MAX = max(dp[i-1][j],dp[i][j-1]); if(A[i] == B[j]){ dp[i][j] = MAX +1; }else{ dp[i][j] = MAX; } } } //输出答案 printf("%dn",dp[m][L]); return 0; }



