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 ...
随机推荐
- sap 给集团分配一个逻辑系统
1.进入事务代码 SALE定义一个新的逻辑系统 2.通过事务代码RZ10 进入之后, 将参数login/no_automatic_user_sapstar 修改为“0”, 然后重启SAP服务生效 3. ...
- BCH/BCHABC/BCHSV分叉后重放机制小结
1. 不过,在谈重放保护之前,我们需要先了解一下重放攻击(又称重播攻击.回放攻击).实际上,重放攻击在互联网行业里就有过出现,也是计算机世界黑客最常用的攻击方式之一,它是指攻击者发送一个目的主机已接收 ...
- 比特币系列钱包的UTXO总结
1.通过比特币钱包的WalletNotify配置来处理 本钱包内的交易信息的 推送.BlockNotify数据更多,不适合交易所的监听
- 【Linux】关于路由跟踪指令traceroute
稍有计算机常识的人都知道ping命令,是用来检查自己的主机是否与目标地址接通,自己的主机与目标地址的通讯包通讯速率,所谓的通讯包也就是那些什么TCP/IP,UDP包,这里说得通俗一点,比如,就拿这 ...
- Android.Study.Question
1. NullPointerException 1.1 发生该异常的原因. 1.2 解决方法有哪几种? try-catch 2. Eclipse 中 debug/run 两个模式,run 是relea ...
- python: "TypeError: 'type' object is not subscriptable"
目前stackoverflow找到两种情况的解决办法: 1.TypeError: 'type' object is not subscriptable when indexing in to a di ...
- H5C3动画
1 渐变 /* 渐变:不同颜色之间的柔和过渡 线性渐变:沿着某条直线发生渐变效果 注意:渐变准备来说是一张背景图 语法:linear-gradient */ background-image: lin ...
- Nginx如何设置禁止IP访问网站
需要禁止IP访问网站.在相关的server中设置相关的限制即可.
- Python 之异常处理机制
python在程序运行出现错误时时有相应的反应机制 ,我们可以针对不同的错误做出不同的响应 list1 = ['a','b','c'] print(list1[4]) #>>>Ind ...
- PHP--根据手机号-淘宝平台获取归属地运营商信息
//获取手机账号信息 public function get_mobile_area($mobile){ $sms = array('province'=>'', 'supplier'=> ...