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. MATLAB单步调试

    我用的是matlab R2012b 1.先设置断点:点击菜单栏中"EDITOR"--"Breakpoints"--"set",出现以下对话框 ...

  2. MVC防止跨站攻击@Html.AntiForgeryToken()

    ASP.NET MVC 中有个标签:@Html.AntiForgeryToken(),需要在页面中加入这个标签,然后在Actoin中加入特性[ValidateAntiForgeryToken]就可以了 ...

  3. jquery的validate表单验证

    html: <form id="reg" action="123.html"> <p class="myerror"> ...

  4. 在Qt(C++)中与Python混合编程

    一.PythonQt库 在Qt(C++)中与Python混合编程,可以使用PythonQt库. 网站首页:http://pythonqt.sourceforge.net 下载页面:https://so ...

  5. 织梦替换ueditor百度编辑器,支持图片水印 教程

    1下载ueditor百度编辑器 2 把下载的zip解压得到ueditor文件夹,把解压到的ueditor文件夹扔进你网站的include文件夹去 3 打开 /include/inc/inc_fun_f ...

  6. Pycharm小知识

    1)  重新更改文件名称:(Shift + F6) 2) 设置IDE皮肤主题 File -> Settings ->  Appearance -> Theme -> 选择“Al ...

  7. L1-033 出生年(15)(STL-set代码)

    L1-033 出生年(15 分) 以上是新浪微博中一奇葩贴:"我出生于1988年,直到25岁才遇到4个数字都不相同的年份."也就是说,直到2013年才达到"4个数字都不相 ...

  8. laravel 门面的介绍和使用

    #上文讲述了laravel中怎么用容器依赖注入类的示例.其实在服务提供者上面在封装一层静态调用,这就是门面.静态调用门面,返回了容器中注册的别名和实例. #下面是测试的示例 #先创建要操作的类 < ...

  9. button获取验证码60秒倒计时 直接用

    __block ; __block UIButton *verifybutton = _GetverificationBtn; verifybutton.enabled = NO; dispatch_ ...

  10. 立即响应ScrollView上的子视图的手势

    self.myScrollView.delaysContentTouches = YES; self.myScrollView.CanCancelContentTouches=NO; 写了一个继承sc ...