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 (≤) 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 (≤) followed by M Eva's favorite color numbers given in her favorite order. Finally the third line starts with a positive integer L (≤) 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:

  1. 6
  2. 5 2 3 1 5 6
  3. 12 2 2 4 1 5 5 6 3 1 1 5 6

Sample Output:

  1. 7

LIS——最长不下降子序列:
算法设计
定义一个长度为N的order数组,将Eva喜欢的颜色编号作为下标,对应元素为次序编号,例如给定喜欢的颜色为2 3 1 5 6,那么order[2]=1,order[3]=2……定义数组A存储给定的珠子序列,定义一个数组dp,其中dp[i]表示以 A[i]结尾的符合要求的子序列最长长度。那么对于0<=i<N,找出0<=j<i之间满足order[i]>=order[j]的最大的dp元素值MAX,则dp[i]=max(MAX,1)。遍历dp数组找出最大的值即为所求。

  1. #include <iostream>
  2. using namespace std;
  3. int like[], color[], dp[];
  4. int main()
  5. {
  6. int n, m, k, res = , x, nums = ;
  7. cin >> n >> m;
  8. for(int i=;i<=m;++i)//给自己喜欢颜色排序标号
  9. {
  10. cin >> x;
  11. like[x] = i;
  12. }
  13. cin >> k;
  14. for (int i = ; i < k; ++i)
  15. {
  16. cin >> x;
  17. if (like[x] > )//删除不是喜欢颜色的变量
  18. color[nums++] = like[x];//记住该颜色的标号
  19. }
  20. for (int i = ; i < nums; ++i)
  21. {
  22. dp[i] = ;//每种颜色单独拼接的长度为1
  23. for (int j = ; j < i; ++j)
  24. if (color[j] <= color[i])//只要前面的颜色的序号比自己小,那么就可以和直接拼接
  25. dp[i] = dp[i] > (dp[j] + ) ? dp[i] : (dp[j] + );
  26. res = res > dp[i] ? res : dp[i];
  27. }
  28. cout << res;
  29. return ;
  30. }

LCS——最长公共子序列
算法设计
定义一个数组A储存喜欢的颜色编号序列,定义一个数组B存储给定的一串珠子序列,找出A、B之间的最长公共子序列即可。由于颜色可以重复,即子序列中可以有重复元素,状态转移方程为:

如果A[i]==B[j],

如果A[i]!=B[j],

边界情况为:

dp[i][0]=dp[0][j]=0(0<=i<=M,0<=j<=L)

  1. using namespace std;
  2. int dp[][];
  3. int main(){
  4. int N,M,L;
  5. scanf("%d%d",&N,&M);
  6. int A[M+]={};
  7. for(int i=;i<=M;++i)
  8. scanf("%d",&A[i]);
  9. scanf("%d",&L);
  10. int B[L+]={};
  11. for(int i=;i<=L;++i)
  12. scanf("%d",&B[i]);
  13. for(int i=;i<=M;++i)
  14. for(int j=;j<=L;++j)
  15. if(A[i]==B[j])
  16. dp[i][j]=max(dp[i][j-],dp[i-][j-])+;
  17. else
  18. dp[i][j]=max(dp[i][j-],dp[i-][j]);
  19. printf("%d",dp[M][L]);
  20. return ;
  21. }

PAT甲级——A1045 Favorite Color Stripe的更多相关文章

  1. PAT甲级1045. Favorite Color Stripe

    PAT甲级1045. Favorite Color Stripe 题意: 伊娃正在试图让自己的颜色条纹从一个给定的.她希望通过剪掉那些不必要的部分,将其余的部分缝合在一起,形成她最喜欢的颜色条纹,以保 ...

  2. PAT 甲级 1045 Favorite Color Stripe

    https://pintia.cn/problem-sets/994805342720868352/problems/994805437411475456 Eva is trying to make ...

  3. PAT 甲级 1045 Favorite Color Stripe(DP)

    题目链接 Favorite Color Stripe 题意:给定$A$序列和$B$序列,你需要在$B$序列中找出任意一个最长的子序列,使得这个子序列也是$A$的子序列 (这个子序列的相邻元素可以重复) ...

  4. PAT 甲级 1045 Favorite Color Stripe (30 分)(思维dp,最长有序子序列)

    1045 Favorite Color Stripe (30 分)   Eva is trying to make her own color stripe out of a given one. S ...

  5. pat 甲级 1045 ( Favorite Color Stripe ) (动态规划 )

    1045 Favorite Color Stripe (30 分) Eva is trying to make her own color stripe out of a given one. She ...

  6. PAT 1045 Favorite Color Stripe[dp][难]

    1045 Favorite Color Stripe (30)(30 分) Eva is trying to make her own color stripe out of a given one. ...

  7. PAT甲级题解(慢慢刷中)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  8. 【转载】【PAT】PAT甲级题型分类整理

    最短路径 Emergency (25)-PAT甲级真题(Dijkstra算法) Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS) Travel P ...

  9. PAT 甲级真题题解(1-62)

    准备每天刷两题PAT真题.(一句话题解) 1001 A+B Format  模拟输出,注意格式 #include <cstdio> #include <cstring> #in ...

随机推荐

  1. 从零学React Native之13 持久化存储

    数据持久化就是指应用程序将某些数据存储在手机存储空间中. 借助native存储 这种方式不言而喻,就是把内容传递给native层,通过原生API存储,详见从零学React Native之05混合开发 ...

  2. SPSS分析:Bootstrap

    SPSS分析:Bootstrap 一.原理: 非参数统计中一种重要的估计统计量方差进而进行区间估计的统计方法,也称为自助法.其核心思想和基本步骤如下: 1.采用重抽样技术从原始样本中抽取一定数量(自己 ...

  3. JS 变量的数据类型转换

    变量的类型转换,一般情况是JS自动转换的,但也有些时候需要手动转换. 1.其它类型转成布尔型 函数和方法的区别 Boolean(a)  //函数是可以独立使用的 document.write(a)   ...

  4. Java性能优化的50个细节,我必须分享给你!

    来源:blog.csdn.net/dongnan591172113/article/details/51790428 ;i<list.size();i++) ,len=list.size();i ...

  5. 用星星画菱形--Java

    用星星画菱形 public class Hello{ public static void main(String[] args) { char star = '\u2605'; System.out ...

  6. 宽域POST提交数据

    小数据宽域可以使用jsonp,但是大数据跨域必须post那么有以下2种方式 1,传统方式 动态生成form var url = ''var $iframe = $("<iframe s ...

  7. 工作中遇到的bug

    1. Error: No PostCSS Config found in.. 在项目根目录新建postcss.config.js文件,并对postcss进行配置: module.exports = { ...

  8. memcache课程---2、php如何操作memcache

    memcache课程---2.php如何操作memcache 一.总结 一句话总结: windows下装好memcache.exe,装好memcache的php扩展之后,然后使用memcache函数库 ...

  9. Excel常用函数总结

    Excel常用函数总结 2016-10-28 Kevin 叼着奶瓶撩妹 1. VLOOKUP函数 常见形式 问题描述:将下图中G列的数据根据学生的姓名填充到D列. 公式解析: =VLOOKUP(A2, ...

  10. 用Spire.PDF提取PDF里的PNG图片

    用Nuget抓取类库,FreeSpire.PDF就可以 代码如下 , 亲测可以抓取PNG图形,即使原图是JPG,也会存成PNG格式输出: //加载PDF文档 PdfDocument doc = new ...