1045 Favorite Color Stripe

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 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
本问题可以看做最长不下降子序列

 #include <iostream>
#include <vector>
#include <map>
using namespace std;
const int maxn = 100005; vector<int> list;
int dp[maxn];
map<int,int> order; int main(){
int n,m,l,tmp;
scanf("%d",&n);
scanf("%d",&m);
for(int i=1;i<=m;i++){
scanf("%d",&tmp);
order[tmp]=i;
}
scanf("%d",&l);
for(int i=0;i<l;i++){
scanf("%d",&tmp);
if(order[tmp]){
list.push_back(order[tmp]);
}
}
int ans=0;
for(int i=0;i<list.size();i++){
dp[i]=1;
for(int j=0;j<i;j++){
if(list[j]<=list[i]&&dp[i]<dp[j]+1){
dp[i]=dp[j]+1;
}
}
ans=max(ans,dp[i]);
}
printf("%d",ans);
}

另外还可以用DFS,但是会有两个点超时

 #include <iostream>
#include <vector>
#include <map>
#include <set>
using namespace std;
const int maxm=;
const int maxn=; int n,m,l;
map<int,int> Mp,checkMap;
int favor[maxn];
vector<int> list;
vector<int> path,tmppath;
set<int> checkSet[maxn];
int maxl=; void DFS(int v,int indx){
tmppath.push_back(v);
if(indx==list.size()-){
if(tmppath.size()>maxl){
maxl=tmppath.size();
}
tmppath.pop_back();
return;
}
for(int i=indx+;i<list.size();i++){
if(checkSet[v].find(list[i])==checkSet[v].end()){
DFS(list[i], i);
}
}
tmppath.pop_back();
return;
} int main(){
int tmp;
scanf("%d",&n);
scanf("%d",&m);
for(int i=;i<m;i++){
scanf("%d",&favor[i]);
Mp[favor[i]]=;
}
for(int i=;i<m;i++){
checkSet[favor[i]]=checkSet[favor[i-]];
checkSet[favor[i]].insert(favor[i-]);
}
//printf("%lu\n",checkSet[favor[4]].size());
scanf("%d",&l);
for(int i=;i<l;i++){
scanf("%d",&tmp);
if(Mp[tmp]) list.push_back(tmp);
} for(int i=;i<list.size();i++){
DFS(list[i],i);
}
printf("%d",maxl);
}

1045 Favorite Color Stripe 动态规划的更多相关文章

  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[dp][难]

    1045 Favorite Color Stripe (30)(30 分) Eva is trying to make her own color stripe out of a given one. ...

  3. PAT甲级1045. Favorite Color Stripe

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

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

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

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

  7. PAT 甲级 1045 Favorite Color Stripe

    https://pintia.cn/problem-sets/994805342720868352/problems/994805437411475456 Eva is trying to make ...

  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)(30 分)

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

随机推荐

  1. 运行PL-SVO(单目)

    代码:https://github.com/rubengooj/pl-svo 1.Prerequisites and dependencies (1)SVO 安装SVO,with ROS:https: ...

  2. HashMap从源码分析数据结构

    1. HashMap在链表中存储的是键值对 2. 数组是一块连续的固定长度的内存空间,再好的哈希函数也不能保证得到的存储地址绝对不发生冲突.那么哈希冲突如何解决呢?哈希冲突的解决方案有多种:开放定址法 ...

  3. DevExpress中RibbonControl的简单应用

    现在介绍一下DevExpress中的RibbonControl的简单应用,搭建一个拥有Office2010风格的窗体程序. 第一步:打开VS2015,新建一个windows窗体应用程序: 第二步:把需 ...

  4. MacDev.GetArchOfLibrary

    1. static library How to check target architecture of a static library http://changhoward.blogspot.c ...

  5. Redhat Linux网卡配置与绑定

    Redhat Linux的网络配置,基本上是通过修改几个配置文件来实现的,虽然也可以用ifconfig来设置IP,用route来配置默认网关,用hostname来配置主机名,但是重启后会丢失. 相关的 ...

  6. GTK图形控件中的rc文件使用心得

    转载自: 1.http://blog.csdn.net/saintwinona/article/details/6972754 2. (1).GTK 主题指南 1.Widgets         GT ...

  7. 设计模式之生成者模式java源代码

    假设要组装一辆自行车,并且自行车就是车轮和车架组成. Builder对应于组装自行车所使用的车轮和车架 ConcreteBuiler对应于自行车的车轮和车架,同时可以返回一辆自行车. Product对 ...

  8. 关于Spring父容器和SpringMvc子容器

    在SSM项目中,会有SpringMvc容器(子容器)和Spring容器(父容器) 一共2个容器 基本规则: 子容器可以访问父容器的bean,父容器不能访问子容器的bean. 当<context: ...

  9. 【UI测试】--规范性

  10. Zxing2.1扫描取景框变形问题解决

    修改竖屏扫描的贴子,2.0之前的都很适用.可是到了2.1,有些贴子的做法可以将扫描框改为竖屏,但是取景框里扫描到的东西是变形的(扁的),本人仔细研究一番,终于解决了这个问题,下面贴出解决办法: 1.修 ...