题目连接:https://www.patest.cn/contests/pat-a-practise/1045原题如下:

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:

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

Sample Output:

7
这道题查看了其他人的,才知道是利用LIS……,难点是怎样将信息提取出来构建一个新的序列,从而可以使用LIS。本题中应EVA最喜欢的color的order重新构成一个序列,进而使用LIS。
 1 #include<stdio.h>
 2 #include<vector>
 3 #include<iostream>
 4 #include<map>
 5 using namespace std;
 6
 7 int main()
 8 {
 9     vector<int>d,input;
10     map<int,int>favorite;
11     int n,N,i,c,M,j;
12     scanf("%d",&n);
13     scanf("%d",&N);
14     for (i=0;i<N;i++)
15     {
16         scanf("%d",&c);
17         favorite[c]=i;
18       //favorite.insert(make_pair(c,i));
19     }
20     scanf("%d",&M);
21     for (i=0;i<M;i++)
22     {
23         scanf("%d",&c);
24         map<int,int>::iterator it=favorite.find(c);
25         if (it!=favorite.end())input.push_back(it->second);  //input中存储的是favorite序号大小,以此来形成LIS(序号之间可以直接比较大小)
26     }
27     int len=input.size(),mm=1;
28     if (len==0){printf("0");return 0;}
29     d.assign(len,1);
30     for (i=1;i<len;i++)    //LIS核心代码
31     {
32         for (j=0;j<i;j++)
33         {
34             if (input[i]>=input[j] && d[i]<d[j]+1)
35             {
36                 d[i]=d[j]+1;
37             }
38         }
39         mm=d[i]>mm?d[i]:mm;
40     }
41     printf("%d",mm);
42     return 0;
43 }

转载于:https://www.cnblogs.com/wuxiaotianC/p/6412576.html

1045. Favorite Color Stripe (30)相关推荐

  1. PAT甲级1045 Favorite Color Stripe (30 分):[C++题解]最佳彩色带、DP、公共子序列变形

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析:这是一个公共子序列的问题.但是有点变式,序列a和序列b不是完全等价的,序列a的每个元素可以对应多个相同元素,而且有些元素可以不使用.比 ...

  2. PAT甲级-1045 Favorite Color Stripe (30分)

    点击链接PAT甲级-AC全解汇总 题目: Eva is trying to make her own color stripe out of a given one. She would like t ...

  3. 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 fa ...

  4. 1045 Favorite Color Stripe (30 分)【难度: 中 / 知识点: DP】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805437411475456 这一道题,实际上就是一道最长上升子序列的一个 ...

  5. pat1045. Favorite Color Stripe (30)

    1045. Favorite Color Stripe (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  6. 1045 Favorite Color Stripe(最长不下降子序列)

    1045 Favorite Color Stripe(最长不下降子序列) 题意:按照题目给出的颜色序列找出原颜色序列中对应的子序列,给出的颜色序列不需要在子序列中全部出现. 解法:根据给出的序列对相应 ...

  7. 【PAT - 甲级1045】Favorite Color Stripe(30分)(dp,LIS类问题)

    题干: Eva is trying to make her own color stripe out of a given one. She would like to keep only her f ...

  8. 1045 Favorite Color Stripe(30分)-PAT甲级

    Eva is trying to make her own color stripe(条纹) out of a given one. She would like to keep only her f ...

  9. 1045 Favorite Color Stripe(LIS解法)

    解题思路 本题属于Longest Increasing Sequence最长不下降子序列,但是要注意,LIS当中不会有无效的元素,而本题是有的,所以先要把无效元素过滤掉,才能转化成为LIS问题. 这里 ...

最新文章

  1. Ubuntu查看硬件详细信息
  2. 浅析Java web程序之客户端和服务器端交互原理
  3. 微博面试Java,微博java开发工程师面试题整理
  4. “开始菜单”按钮今年8月将重回Windows 8
  5. mysql事务实现数据更新_MySql事务select for update及数据的一致性处理讲解
  6. pyqt5中sender方法介绍_【第五节】PyQt5事件和信号
  7. Github 数据洞察之复杂信息网络
  8. git 修改远程仓库地址
  9. centos7设置静态IP地址
  10. cmd jar 无效_为什么我的JAR文件以CMD执行,而不是双击执行?
  11. Android学习笔记之AndroidManifest.xml文件解析(摘自皮狼的博客)
  12. 190317每日一句
  13. nuxt SSR部署到iis7方案
  14. linux dev/zero,/dev/zero是什么意思
  15. 矩阵奇异值分解(详解)
  16. 数据交换技术:OPC技术工作原理简述
  17. 微信公众号jsapi支付
  18. 逗号运算符java_简单的java计算器 实现了重复标点及运算符连点限制
  19. 弱网测试之NEWT(Network Emulator Toolkit)
  20. STC89C52单片机定时器及中断系统的介绍以及代码示例

热门文章

  1. python怎么索引txt数据中第四行_python-在熊猫数据框中按行计数编制索引
  2. java动态字段排序_Java8对多个字段排序
  3. python数据加载常规教程_Python加载数据的5种不同方式(收藏)
  4. 时间字符串转时间戳_Python3日期与时间戳转换的几种方法
  5. 为什么不敢和别人竞争_孩子在学校不敢竞争?你该怎么做
  6. daocloud创建mysql_DaoCloud体验-使用node构建应用程序
  7. c ++结构体构造函数_C ++中的构造函数
  8. jsf如何与数据库连接_JSF数据库示例– MySQL JDBC
  9. 小程序nginx做反向代理_NGINX作为节点或Angular应用程序的反向代理
  10. 如何在Linux中使用netstat命令