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. Lumen开发:结合Redis实现消息队列(1)

    1.简介 Lumen队列服务为各种不同的后台队列提供了统一的API.队列允许你推迟耗时任务(例如发送邮件)的执行,从而大幅提高web请求速度. 1.1 配置 .env文件的QUEUE_DRIVER选项 ...

  2. C语言中的编译时分配内存

    1.栈区(stack) --编译器自动分配释放,主要存放函数的参数值,局部变量值等: 2.堆区(heap) --由程序员分配释放: 3.全局区或静态区 --存放全局变量和静态变量:程序结束时由系统释放 ...

  3. 阿里云+LAMP环境配置

    1. 准备好一键Linux环境的脚本: http://dwz.cn/6Nlexm 2. 运行命令:# yum install lynx tree nmap sysstat lrzsz dos2unix ...

  4. js城市联动选择器

    <html> <head> <META charset="utf8"> <script type="text/javascrip ...

  5. Python菜鸟之路:JQuery基础

    前言 JQuery可以理解为是一个模块,里边封装了DOM以及JavaScript,可以方便的对JQuery对象进行操作. 版本 尽量选择1.X系统的Jquery版本,例如1.12.jquery.js. ...

  6. SM30 表格维护生成器

    1)SE11创建自建表,结构如下: 2) 创建表维护 3) 针对上面创建的函数组ZMM_MAT_DESC,做以下增强处理 添加的Module 代码如下: module mod_customize in ...

  7. Vue:实践学习笔记(1)——快速使用

    Vue:实践学习笔记(1)——快速使用 Vue基础知识 0.引入Vue 官方地址:Vue的官方下载地址 Vue推荐博客:keepfool 在你的程序中快速引入Vue: <!-- 开发环境版本,包 ...

  8. 实用篇:说说我在JavaScript项目中使用的工具类

    在JavaScript的开发中,我们都会写一些工具类来帮我们简化一些业务操作的逻辑,一下就貼几个我在项目开发过程中常用的工具类.表达能力有限,各位看官还是看源码吧. 一.日期处理工具类. /** * ...

  9. mac manpages 汉化

    默认在终端进行man命令,如:man ls,会显示英文的帮助文档.本文教你如何查看中文文档. 资源:1.manpages-zh-1.5.2.tar.bz22.groff-1.21.tar.gz   - ...

  10. 算法(Algorithms)第4版 练习 2.2.11(1)

    实现关键代码: private static void sort(Comparable[] input, int lo, int hi) { if((lo+CUTOFF-1) >= hi) { ...