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. Java线程面试题:设计四个线程,其中两个线程每次对 j 加 1,另外两个每次对 j 减 1,程序如下。

    package thread; /** * Java线程面试题 * @author zhongfg * @date 2015-06-16 */ public class ThreadInterview ...

  2. yii2.0 console执行php守护进程

    //该方法只需执行一次public function actionIndex(){ $pid =pcntl_fork();//在当前进程中生成一个新的子进程 //$pid会有三种形式 $pid==-1 ...

  3. maven-tomcat7;IOC;AOP;数据库远程连接

    [说明]真的是好烦下载插件啊,maven-tomcat7 插件试了好多次都不行,下载不成:部署不成:好不容易从github中得到的springmvc项目也是运行不起来,中间又是查了许多东西,绕着绕着都 ...

  4. CF1060 E-Sergey and Subway

    题目戳这里 一句话题意 一棵树,任意相隔一个点的两个点连一条新边(原边留下),问所有点对的距离之和. Solution 本来看见是黑题有点怕,但仔细一想也没有那么难. 先处理出每个点的深度(dep)和 ...

  5. java线程 同步临界区:thinking in java4 21.3.5

    java线程 同步临界区:thinking in java4 21.3.5 thinking in java 4免费下载:http://download.csdn.net/detail/liangru ...

  6. eclipse安装Activiti Designer插件(转载:http://blog.csdn.net/qq_33547950/article/details/54926435)

    为了完成毕业设计,需要学习Activiti.万事开头难,果然刚开始就遇到了问题.<Activiti实战>和视频教程里提供的安装Activiti Designer插件方法(即下文方法一)不能 ...

  7. kNN算法及其python&R实现

    iris数据集,这一教科书级别的数据,分类前不需要做任何数据预处理什么的,相当的理想!但请注意你自己的数据99%的可能需要做预处理. 下面分别用R语言和Python来实现iris数据集的分类: R语言 ...

  8. 关于树莓派Pi2通过UART连接攀藤G5传感器的python

    1.准备工作:树莓派Pi2板子,攀藤G5传感器 关于树莓派40pin口网上很多,我们只了解与攀藤G5连接的问题 (1)攀藤G5pin1(VCC5v)要注意是5V,有很多板子接的是3V,而树莓派的pin ...

  9. flex 特性

    flex grow 分配的比例是对整体,比如 A grow 3  B grow 4,A连同margin一块是3

  10. JS中如何获取<Select>中value和text的值

    原文地址:JS中如何获取<Select>中value和text的值 html代码: <select id = "city" onchange="chan ...