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 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把喜欢的都标记为1,然后裁剪时,只把喜欢的加入序列,对于第i个,找到前面最近的且不是排在他后边的来更新当前的最大长度。所以一开始要标记一下check[a][b]如果是1,表示a在b前,如果是-1,表示a在b后,如果是0就是相等的。
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
int n,m,l,like_order[],test[];
int check[][],iflike[],dp[],ma;
int main() {
scanf("%d",&n);
scanf("%d",&m);
for(int i = ;i < m;i ++) {
scanf("%d",&like_order[i]);
iflike[like_order[i]] = ;
for(int j = ;j < i;j ++) {///标记前后
check[like_order[j]][like_order[i]] = ;
check[like_order[i]][like_order[j]] = -;
}
}
scanf("%d",&l);
for(int i = ;i < l;i ++) {
scanf("%d",&test[i]);
if(!iflike[test[i]]) {///如果不是喜欢的直接过了,并且不保存,如果用vector,就直接不加入序列
i --;
l --;
continue;
}
int k = i - ;
while(k >= && check[test[k]][test[i]] == -) {
k --;
}///只要不是排在后边的 都可以放在前边
dp[i] ++;///自己本身长度为1
if(k >= )dp[i] += dp[k];///如果 存在就加上他的长度
ma = max(ma,dp[i]);///更新最大
}
printf("%d",ma);
}
这道题如果标记喜欢的颜色的位置,就是求最长非递减子序列。
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
int n,m,l,pos[];
int dp[],d,c;
int main() {
scanf("%d",&n);
scanf("%d",&m);
for(int i = ;i <= m;i ++) {
scanf("%d",&d);
pos[d] = i;
}
scanf("%d",&l);
for(int i = ;i < l;i ++) {
scanf("%d",&d);
if(!pos[d])continue;
if(!c || pos[dp[c - ]] <= pos[d])dp[c ++] = d;
else {
int l = ,r = c - ,mid;
while(l < r) {
mid = (l + r) / ;
if(pos[dp[mid]] <= pos[d])l = mid + ;
else r = mid;
}
dp[l] = d;
}
}
printf("%d",c);
}
1045 Favorite Color Stripe (30)(30 分)的更多相关文章
- 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 (30 分)(思维dp,最长有序子序列)
1045 Favorite Color Stripe (30 分) Eva is trying to make her own color stripe out of a given one. S ...
- 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. ...
- 1045 Favorite Color Stripe 动态规划
1045 Favorite Color Stripe 1045. Favorite Color Stripe (30)Eva is trying to make her own color strip ...
- PAT甲级1045. Favorite Color Stripe
PAT甲级1045. Favorite Color Stripe 题意: 伊娃正在试图让自己的颜色条纹从一个给定的.她希望通过剪掉那些不必要的部分,将其余的部分缝合在一起,形成她最喜欢的颜色条纹,以保 ...
- 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 ...
- 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)
Eva is trying to make her own color stripe out of a given one. She would like to keep only her favor ...
随机推荐
- RecyclerView的使用(3)之加入Header和Footer
原创文章.转载请注明 http://blog.csdn.net/leejizhou/article/details/50742544 李济洲的博客 RecyclerView尽管作为ListView的替 ...
- 百度地图SnapshotReadyCallback截屏
今天碰到了地图截图的功能,不太会,查查资料知道怎么弄了,跟大家分享一下 直接上代码,弄了一个方法,将截取的图片上传至服务器,返回给我们图片路径 //获取地图截图 private void getscr ...
- Win7程序运行出现Windows Based Scrip Host 已停止工作问题的解决方法
最近在使用类似于Teamviewer这样的程序时,突然运行不了,报错:Microsoft Windows Based Scrip Host 已停止工作的问题,从系统日志来看,好像是空指针问题,但是又无 ...
- api签名认证
参数列表: data: { sign, uid或是openId, version, timestamp, param } sign 签名一般情况下,根据如下几项生成,通过md5或是aes加密: 接口 ...
- git reset和git revert
1 git reset commit-id 直接回到某次提交,该次commit-id之后的提交都会被删除. --hard,将index和本地都恢复到指定的commit版本. 2 git revert ...
- LLVM编译器
LLVM 1. 说说 LLVM(Low Level Virtual Machine)到底是什么吧 先说编译器:编译器是把程序员的代码翻译成机器可以理解的语言的工具: 再谈 LLVM:一个模块化和可重用 ...
- ASP获取上月本月下月的第一天和最后一天
上月第一天:<%=dateadd("m",-1,year(date)&"-"&month(date)&"-1" ...
- 3.写一个hello world页面
经过1和2的学习,现在已经可以正常启动Django了,这一节说怎么写一个hello world页面,所有的环境基础就是1和2中搭建的 1.在app模块中添加页面 具体为 hello_django\he ...
- socket编程python+c
python版: server: def socket_loop_server_function(): HOST = '192.168.56.1' PORT = 21567 sk = socket.s ...
- VLFeat图像库在VS2012下的配置
近期做课题所需,開始使用VLFeat图像库. 库下载链接: http://download.csdn.net/detail/sunboyiris/7500097 ...