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<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = ;
const int maxc = ;
int hashTable[maxn];
int A[maxc],dp[maxc]; //数组A和dp的大小和L有关
int main(){
int n,m,x;
memset(hashTable,-,sizeof(hashTable));
scanf("%d%d",&n,&m);
for(int i = ; i < m; i++){
scanf("%d",&x);
hashTable[x] = i;
}
int L,num = ;
scanf("%d",&L);
for(int i = ; i < L; i++){
scanf("%d",&x);
if(hashTable[x] >= ){
A[num++] = hashTable[x];
}
}
int ans = -;
for(int i = ; i < num; i++){
dp[i] = ;
for(int j = ; j < i; j++){
if(A[i] >= A[j] && dp[i] < dp[j] + ){
dp[i] = dp[j] + ;
}
}
ans = max(ans,dp[i]);
}
printf("%d",ans);
return ;
}
//最长公共子序列
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = ;
const int maxL = ;
int dp[maxL][maxL],A[maxn],B[maxL];
int main(){
int n,m,x;
scanf("%d%d",&n,&m);
for(int i = ; i <= m; i++){ //[1,n]
scanf("%d",&A[i]);
}
int L;
scanf("%d",&L);
for(int i = ; i <= L; i++){ //[1,L]
scanf("%d",&B[i]);
}
for(int i = ; i <= m; i++){ //[0,m]
dp[i][] = ;
}
for(int i = ; i <= L; i++){ //[0,L] 四个取值范围存疑
dp[][i] = ;
}
for(int i = ; i <= m; i++){
for(int j = ; j <= L; j++){
int MAXV = max(dp[i-][j],dp[i][j-]);
if(A[i] == B[j]){
dp[i][j] = MAXV+;
}else{
dp[i][j] = MAXV;
}
}
}
printf("%d",dp[m][L]);
return ;
}
1045 Favorite Color Stripe (30)的更多相关文章
- 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) -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)(30 分)
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分)(简单dp)
Eva is trying to make her own color stripe out of a given one. She would like to keep only her favor ...
- 【PAT甲级】1045 Favorite Color Stripe (30 分)(DP)
题意: 输入一个正整数N(<=200),代表颜色总数,接下来输入一个正整数M(<=200),代表喜爱的颜色数量,接着输入M个正整数表示喜爱颜色的编号(同一颜色不会出现两次),接下来输入一个 ...
- PAT (Advanced Level) 1045. Favorite Color Stripe (30)
最长公共子序列变形. #include<iostream> #include<cstring> #include<cmath> #include<algori ...
- 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[dp][难]
1045 Favorite Color Stripe (30)(30 分) Eva is trying to make her own color stripe out of a given one. ...
随机推荐
- 全栈项目|小书架|服务器开发-Koa2中间件机制洋葱模型了解一下
KOA2 是什么? Koa是一个新的 web 框架,由 Express 幕后的原班人马打造, 致力于成为 web 应用和 API 开发领域中的一个更小.更富有表现力.更健壮的基石. 通过利用 asyn ...
- VisualStudio 连接 MySql 实现增删查改
首先创建数据库,建立一个用户登录表 2.visualStudio默认是不支持MySql的,要想通过Ado.Net 操作MySql 需要在管理NeGet包添加对MySql.Data 和 MySql.D ...
- C# 使用代理实现方法过滤
一.为什么要进行方法过滤 一些情况下我们需要再方法调用前记录方法的调用时间和使用的参数,再调用后需要记录方法的结束时间和返回结果,当方法出现异常的时候,需要记录异常的堆栈和原因,这些都是与业务无关的代 ...
- Vue 使用技巧手记
watch监听 Vue监听属性有很多种写法,也有几个配置选项 使用配置项 new Vue({ watch:{ content:{ handler(old,newVal){ console.log(ol ...
- C语言内存管理(内存池)
C语言可以使用alloc从栈上动态分配内存. 内存碎片 Malloc/free或者new/delete大量使用会造成内存碎片,这种碎片形成的机理如下: 内存碎片一般是由于空闲的内存空间比要连续申请的空 ...
- Linux下 启动tomcat 时候同时日志命令
./startup.sh && tail -f ../logs/catalina.out
- K-D树详解
K-D树最近邻算法https://blog.csdn.net/image_fzx/article/details/80624968 一般说来,索引结构中相似性查询有两种基本的方式: 一种是范围查询,范 ...
- Java8 Stream 流使用场景和常用操作
JAVA8内置的函数式编程接口应用场景和方式 pojo类对象和默认创建list的方法 import lombok.AllArgsConstructor; import lombok.Data; imp ...
- java中List集合
List集合是一个元素有序可以重复的集合,集合中每个元素都有其对应的顺序索引.List集合允许使用重复集合,前面博客写到Set不允许有重复集合.List集合可以通过索引来访问指定位置的集合元素. Li ...
- 十七.protobuf在rpc中的使用
关于protobuf在rpc中的使用,设计到gRPC,相关内容待续....