PAT 1045. Favorite Color Stripe (30) 变种LCS或LIS

来源:互联网 发布:淘宝企业店铺公司更改 编辑:程序博客网 时间:2024/06/03 18:18

1045. Favorite Color Stripe (30)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 (<=10000) which is the length of the given stripe, followed by L colors on the stripe. All the numbers in a line are 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
先放别人的东西。这个图好评

题意:

题目抽象出来就是寻找两个序列的最长公共子序列, 但是公共部分允许元素重复出。

分析:

最长公共子序列(LCS,Longest Common Subsequence)的变种-公共部分可以元素重复。



代码:

[cpp] view plain copy
  1. #include <iostream>  
  2. #include <fstream>  
  3. #include <algorithm>  
  4. #include <vector>  
  5. #include <cstdio>  
  6. #include <cstring>  
  7.   
  8. using namespace std;  
  9.   
  10. //此代码使用前,需删除下面两行+后面的system("PAUSE")  
  11. ifstream fin("in.txt");  
  12. #define cin fin  
  13.   
  14. int like[201]={0};  
  15. int given[10001]={0};  
  16. int len[201][10001]={0};  
  17.   
  18. int LCS(int row,int col)  
  19. {  
  20.     int i,j;  
  21.     int max;  
  22.     for(i=1;i<=row;i++){  
  23.         for(j=1;j<=col;j++){  
  24.             max = len[i-1][j-1];  
  25.             if(max < len[i][j-1])max = len[i][j-1];  
  26.             if(max < len[i-1][j])max = len[i-1][j];      //先求出左边、上边、左上边 三个值中的最大值  
  27.             if(like[i]==given[j]){              //如果相等,则将最大值+1  
  28.                 len[i][j] = max+1;  
  29.             }else{  
  30.                 len[i][j] = max;  
  31.             }             
  32.         }  
  33.     }  
  34.     return len[row][col];  
  35. }  
  36.   
  37.   
  38. int main()  
  39. {  
  40.     int n,m,l;  
  41.     cin>>n>>m;  
  42.     int i;  
  43.     for(i=0;i<m;i++)cin>>like[i+1];  
  44.     cin>>l;  
  45.     for(i=0;i<l;i++)cin>>given[i+1];  
  46.   
  47.     cout<<LCS(m,l)<<endl;  
  48.   
  49.     system( "PAUSE");  
  50.     return 0;  
  51. }  
因为可以横着和竖着转移了。其实只需要从dp[i-1][j]和dp[i][j-1]寻找最大者就可以了,不需要考虑dp[i-1][j-1]。附上我的代码:
#include <iostream>#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int a[201];int b[10001];int dp[201][10001]={0};int n,m,l;void lcs() {     for(int row=1;row<=m;row++)      for(int col=1;col<=l;col++)     {         int tmp=max(dp[row-1][col],dp[row][col-1]);              if(a[row]==b[col]) dp[row][col]=tmp+1;              else dp[row][col]=tmp;     } }int main(){    cin>>n>>m;    for(int i=1;i<=m;i++)     cin>>a[i];    cin>>l;    for(int i=1;i<=l;i++)        cin>>b[i];        lcs();    cout<<dp[m][l];    return 0;}

再附上一个别人LIS的代码

先把喜欢的序列给编号:
喜欢颜色序列:2 3 1 5 6
喜欢颜色编号:1 2 3 4 5
然后把它对应到已有序列上面去
原颜色序列 2 2 4 1 5 5 6 3 1 1 5 6 
最后的max:[1] [2] 0 [3] [4] [5] [6] [3] [4] [5] [6] [7]
映射之后的: 1 1 0 3 4 4 5 2 3 3 4 5 
因为我们知道它一定要按照序列排,也就是说最后的序列一定要去递增的

时间复杂度是O(M*L)。

AC代码:

[cpp] view plain copy
  1. #include<stdio.h>  
  2. #include<string.h>  
  3. using namespace std;  
  4. int stripe[10002];//存放彩带  
  5. int like[202];//存放喜欢序列  
  6. int main(){  
  7.  //freopen("F://Temp/input.txt", "r", stdin);  
  8.  int n;  
  9.  scanf("%d", &n);  
  10.  int n_like;//喜欢的颜色序列个数  
  11.  memset(like, 0, sizeof(like));  
  12.  memset(stripe, 0, sizeof(stripe));//init  
  13.  scanf("%d", &n_like);  
  14.  for (int i = 1; i <= n_like; i++){  
  15.   int tmp;  
  16.   scanf("%d", &tmp);  
  17.   like[tmp] = i;//喜欢颜色序列做一个映射  
  18.  }  
  19.  int num;  
  20.  scanf("%d", &num);  
  21.  for (int i = 1; i <= num; i++){  
  22.   int tmp;  
  23.   scanf("%d", &tmp);  
  24.   stripe[i] = like[tmp];//变成映射后的彩带序列  
  25.  }  
  26.  int max;  
  27.  int big[1002];  
  28.  memset(big, 0, sizeof(big));  
  29.  for (int i = 1; i <= num; i++){  
  30.   if (stripe[i] == 0)continue;//表示当前颜色不在喜欢的颜色序列中  
  31.   max = 0;  
  32.   for (int j = 1; j <= stripe[i]; j++){  
  33.    if (max <= big[j]){//找到当前颜色之前累计数量最多的颜色  
  34.     max = big[j];  
  35.    }  
  36.   }  
  37.   big[stripe[i]] = max + 1;//把当前颜色添加到最多的颜色之后,形成最长序列  
  38.  }  
  39.  max = 0;  
  40.  for (int i = n_like; i >= 1; i--){//找到最大值  
  41.   if (big[i] > max)max = big[i];  
  42.  }  
  43.  printf("%d\n", max);  
  44.  return 0;  
  45. }