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. extendgcd模板

    看了数论第一章,终于搞懂了扩展欧几里德,其实就是普通欧几里德的逆推过程. // ax+by = gcd(a,b) ->求解x,y 其中a,b不全为0,可以为负数// 复杂度:O(log2a)vo ...

  2. 【BZOJ2081】[Poi2010]Beads hash+调和级数

    [BZOJ2081][Poi2010]Beads Description Zxl有一次决定制造一条项链,她以非常便宜的价格买了一长条鲜艳的珊瑚珠子,她现在也有一个机器,能把这条珠子切成很多块(子串), ...

  3. The hidden layers are either convolutional, pooling or fully connected.

    https://en.wikipedia.org/wiki/Convolutional_neural_network Convolutional layers apply a convolution ...

  4. ajax (异步js+xml)

    AJAX 基础 AJAX = 异步js+xml 通过与后台服务器进行少量数据交换,实现前台网页局部更新 XMLHttpRequest对象 是 AJAX 的基础 var xmlhttp; if (win ...

  5. 我的Android进阶之旅------>Android嵌入图像InsetDrawable的用法

    面试题:为一个充满整个屏幕的LinearLayout布局指定背景图,是否可以让背景图不充满屏幕?请用代码描述实现过程. 解决此题,可以使用嵌入(Inset)图像资源来指定图像,然后像使用普通图像资源一 ...

  6. 从 零开始 无差错 装好nginx+PHP

    由于这两天 一直有人追问 nginx为何报错,为何php没装好啥的,大多原因是 : 1.编译与yum混合安装,导致很多包的路径不对,进而报错 2.yum源比较旧,导致 与新版本的php不匹配 3.安装 ...

  7. eclispse + tomcat 启动是不加载项目的解决办法

    有一个java spring的项目一直好好的,突然一天不能启动了.eclipse的console没有报任何错误,相关的server配置也没有问题,经过一翻折腾顺便还把eclipse从indigo升级到 ...

  8. 简单做出HTML5翻页效果文字特效

    之前在网上看到一款比较有新意的HTML5文字特效,文字效果是当鼠标滑过是出现翻开折叠的效果,类似书本翻页.于是我兴致勃勃的点开源码看了一下,发现其实实现也挺简单的,主要利用了CSS3的transfor ...

  9. stm32非操作系统开发和带uCos的开发的区别,及一些解析

    从文件角度来看core_cm4.h和stm32f4xx.h分别从内核寄存器和外设寄存器来定义其地址和结构体,是用c语言访问硬件必须的文件,所以这两个文件不论是否带操作系统,都是必须包含进工程的. re ...

  10. 多图切换jQuery图片滑块插件

    在线演示 本地下载