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 ...
随机推荐
- Linux 下SHELL脚本自动同步文件
#!/bin/bash expect <<EOF set timeout spawn rsync -avz root@192.168.10.57:/var/www/html/manage_ ...
- 将文件转换成byte[]数组
代码 /// <summary> /// 将文件转换成byte[] 数组 /// </summary> /// <param name="fileUrl&quo ...
- Memcached使用与纠错(附代码和相关dll)
今天没事研究一下,谁想到遇到了几个dll找不到,网上也不好找到,索性功夫不负有心人.贴出代码和相关的dll Memcached代码:(网上都是的,很多人都保存了这个代码) using Memcache ...
- [z]Linux下压缩与解压
1.压缩命令: 命令格式:tar -zcvf 压缩文件名.tar.gz 被压缩文件名 可先切换到当前目录下.压缩文件名和被压缩文件名都可加入路径. 2.解压缩命令: 命令格式:tar -z ...
- pycharm 出现Process finished with exit code 0 或 Process finished with exit code -1
Process finished with exit code 0 意味着你的程序正常执行完毕并退出. 可以科普一下exit code,在大部分编程语言中都适用: exit code 0 表示程序执行 ...
- c# Parallel.For 并行编程 执行顺序测试
因为有个for 实际执行结果尽管是按照for里边的顺序执行,但处理器让哪个分线程先执行,谁先处理完就不一定了. 对于要求结果需要先后顺序的,比如对text内容的操作, 用并行 Parallel.For ...
- dev accordionControl panelcontorl 使用
对于dev的窗体布局我想更系统的专业的学学,不是评自己以往 的经验去做, 所以我看了dev的demo 里边的例子,封装的很严实,还有他们自己重新做的控件,无法直接使用, 关键的控件也上了锁,可能也是保 ...
- 抽象类,override,final和类模板
抽象类: **有些函数由于信息不够具体,而无法实现** 由此而来的纯虚函数:在基类中声明的纯虚函数,在基类中无法实现(是因为在基类中定义的信息不够具体,不是学的知识不够),于是这个函数没办法规定具体的 ...
- grep如何进行正则表达式查找
字符类 字符类的搜索:如果我想要搜寻 test 或 taste 这两个单字时,可以发现到,其实她们有共通的 't?st' 存在-这个时候,我可以这样来搜寻: [root@www ~]# grep -n ...
- linux系统中的进程状态分析
转载地址:https://blog.csdn.net/shenwansangz/article/details/51981459 linux是一个多用户,多任务的系统,可以同时运行多个用户的多个程序, ...