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
把喜欢的都标记为1,然后裁剪时,只把喜欢的加入序列,对于第i个,找到前面最近的且不是排在他后边的来更新当前的最大长度。所以一开始要标记一下check[a][b]如果是1,表示a在b前,如果是-1,表示a在b后,如果是0就是相等的。
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
int n,m,l,like_order[],test[];
int check[][],iflike[],dp[],ma;
int main() {
scanf("%d",&n);
scanf("%d",&m);
for(int i = ;i < m;i ++) {
scanf("%d",&like_order[i]);
iflike[like_order[i]] = ;
for(int j = ;j < i;j ++) {///标记前后
check[like_order[j]][like_order[i]] = ;
check[like_order[i]][like_order[j]] = -;
}
}
scanf("%d",&l);
for(int i = ;i < l;i ++) {
scanf("%d",&test[i]);
if(!iflike[test[i]]) {///如果不是喜欢的直接过了,并且不保存,如果用vector,就直接不加入序列
i --;
l --;
continue;
}
int k = i - ;
while(k >= && check[test[k]][test[i]] == -) {
k --;
}///只要不是排在后边的 都可以放在前边
dp[i] ++;///自己本身长度为1
if(k >= )dp[i] += dp[k];///如果 存在就加上他的长度
ma = max(ma,dp[i]);///更新最大
}
printf("%d",ma);
}

这道题如果标记喜欢的颜色的位置,就是求最长非递减子序列。

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
int n,m,l,pos[];
int dp[],d,c;
int main() {
scanf("%d",&n);
scanf("%d",&m);
for(int i = ;i <= m;i ++) {
scanf("%d",&d);
pos[d] = i;
}
scanf("%d",&l);
for(int i = ;i < l;i ++) {
scanf("%d",&d);
if(!pos[d])continue;
if(!c || pos[dp[c - ]] <= pos[d])dp[c ++] = d;
else {
int l = ,r = c - ,mid;
while(l < r) {
mid = (l + r) / ;
if(pos[dp[mid]] <= pos[d])l = mid + ;
else r = mid;
}
dp[l] = d;
}
}
printf("%d",c);
}

1045 Favorite Color Stripe (30)(30 分)的更多相关文章

  1. 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 ...

  2. 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 ...

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

  4. 1045 Favorite Color Stripe 动态规划

    1045 Favorite Color Stripe 1045. Favorite Color Stripe (30)Eva is trying to make her own color strip ...

  5. PAT甲级1045. Favorite Color Stripe

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

  6. 1045 Favorite Color Stripe (30分)(简单dp)

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

  7. 1045. Favorite Color Stripe (30) -LCS允许元素重复

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

  8. 1045. Favorite Color Stripe (30) -LCS同意元素反复

    题目例如以下: Eva is trying to make her own color stripe out of a given one. She would like to keep only h ...

  9. 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 favor ...

随机推荐

  1. 【BZOJ5037】[Jsoi2014]电信网络 最大权闭合图

    [BZOJ5037][Jsoi2014]电信网络 Description JYY创建的电信公司,垄断着整个JSOI王国的电信网络.JYY在JSOI王国里建造了很多的通信基站.目前所有的基站都是使用2G ...

  2. Largest Rectangle in a Histogram (最大子矩阵)

    hdu 1506 A histogram is a polygon composed of a sequence of rectangles aligned at a common base line ...

  3. HDFS上传机制

  4. iOS和Android后台机制对比

    转自:http://blog.csdn.net/zsch591488385/article/details/27232881 一.iOS的“伪后台”程序 首先,先了解一下ios 中所谓的「后台进程」到 ...

  5. html5plus (H5 WebApp)

    是什么? 它是增强版的手机浏览器引擎, 让HTML5达到原生水平, 它提供WebApp的规范. 它结合MUI(前端框架) + HBuilder(开发工具) 即可迅速实现开发一个app. 快速起步? 1 ...

  6. 【python】-- 继承式多线程、守护线程

    继承式多线程 1.定义 继承式多线程是自己自定义类,去继承theading.Tread这个类,通过类实例.start()启动,去执行run方法中的代码. import threading import ...

  7. Write 语句

    ABAP Write 语句 转载▼http://blog.sina.com.cn/s/blog_5ccd375b0100ghhi.html   1.Write 叙述 ABAP/4 用来在屏幕上输出数据 ...

  8. JETSON TK1 ~ 基于eclipse下开发ROS

    此文档是在PC端开发后移植到TK1,并非在TK1上安装eclipse 官方使用IDE开发的文档: http://wiki.ros.org/IDEs 一:安装eclipse 1.下载eclipse安装包 ...

  9. HDU - 5695 Gym Class 【拓扑排序】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5695 思路 给定一些关系 进行拓扑排序 但是有一个要求 对于哪些没有确切的位置的点 要按照ID大小 I ...

  10. [原创]java WEB学习笔记29:Cookie Demo 之自动登录

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...