1045 Favorite Color Stripe 动态规划
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 动态规划的更多相关文章
- 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 ...
- 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. ...
- PAT甲级1045. Favorite Color Stripe
PAT甲级1045. Favorite Color Stripe 题意: 伊娃正在试图让自己的颜色条纹从一个给定的.她希望通过剪掉那些不必要的部分,将其余的部分缝合在一起,形成她最喜欢的颜色条纹,以保 ...
- 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 ...
- 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 ...
- 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 ...
- PAT 甲级 1045 Favorite Color Stripe
https://pintia.cn/problem-sets/994805342720868352/problems/994805437411475456 Eva is trying to make ...
- 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 ...
- 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 ...
随机推荐
- YII2中使用控制台命令
有些时候我们需要通过crontab在后台跑一些定时脚本,这时候就需要用到控制台命令了. 我们在commands目录下创建TestController.php,当然脚本的位置是可以随意指定的,只需要在c ...
- html5的地理位置定位
html5提供的地理位置定位使开发人员不用借助其他软件就能轻松实现位置查找,地图应用,导航等功能. 地理位置定位基本原理GPS, WIFI, IP, 手机信号基站 核心对象Geolocation是wi ...
- Memcached使用与纠错(附代码和相关dll)
今天没事研究一下,谁想到遇到了几个dll找不到,网上也不好找到,索性功夫不负有心人.贴出代码和相关的dll Memcached代码:(网上都是的,很多人都保存了这个代码) using Memcache ...
- [z]Libevent使用例子,从简单到复杂
[z]http://blog.csdn.net/luotuo44/article/details/39670221 本文从简单到复杂,展示如何使用libevent.网上的许多例子都是只有服务器端的,本 ...
- eclipse导入项目以后,内容没有错误,项目上却有个小红叉?
对于上面的错误,应该如何解决?
- 初次搭建spring-boot 整合ssm(有许多小坑)
首先,我是采用官网下载,版本最好选择1.5.16的(这是重点) 下载完毕后,用idea打开解压后的项目. 1.整合spring-mvc 在pom.xml中加入web依赖 <dependency& ...
- UOJ 67 新年的毒瘤 - Tarjan
Description 给出一个无向图, 要求找出某个点$u$, 去掉$u$和$u$所连的边, 所剩下的节点构成一棵树. Solution 首先, 割点肯定是不可能满足条件的, 因为去掉割点后会构成若 ...
- POJ 2135.Farm Tour 消负圈法最小费用最大流
Evacuation Plan Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4914 Accepted: 1284 ...
- 数组转xml格式/xml格式转数组
数组转xml格式 $arr=array( 'username'=>'huahua', 'password'=>'123456', 'number'=>'15889652911', ) ...
- String、StringBuffer、StringBuilder和StringTokenizer的区别
1)String.StringBuffer.StringBuilder都用于字符串操作,其中,String是不可变类,即String对象一旦被创建,其值不能被修改,而StringBuffer和Stri ...