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. ...
随机推荐
- IDEA使用@Data注解,类调用get、set方法标红的解决办法
1.在setting中,下载lombok插件,安装完成后重启idea
- 后台传带引号(")的数据需要注意
后台返回给前端的json字符串 [{"\"Name\":\"<span style=\\\"color: red\\\">&qu ...
- GitHub上传文件问题总结
问题一:git warning: LF will be replaced by CRLF in 解决办法 在Git Bash中输入git add .时出现上述语句. 解决办法: 输入以下语句: $ g ...
- Qt环境搭建
下载 qtcreator:http://download.qt.io/official_releases/qtcreator/ 编译器(mingw):http://download.qt.io/dev ...
- 利用ABAP 740的新关键字REDUCE完成一个实际工作任务
ABAP 740从2013年发布至今已经过去很长的时间了,下面这张图来自SAP社区博客: ABAP News for Release 7.40 – What is ABAP 7.40? 图中的ABAP ...
- Mysql中decode函数的几种用法
1.使用decode判断字符串是否一样 decode(value,if1,then1,if2,then2,if3,then3,...,else) 含义为 IF 条件=值1 THEN RETURN(va ...
- Python学习日记(二十三) 类命名空间和组合
类命名空间 在一个类中它的函数(方法)属于动态属性,直接定义的变量属于静态属性 首先先定义一个类,并在这个类里面加入静态变量.属性等然后将一个对象实例化 class Fighter: #定义一个战机的 ...
- postgressql启动与关闭
POSTGRESSQL启动和停止数据库可以通过service方式 .POSTGRESSQL的命令行工具进行启动与停止. 1.使有SERVICE方式 启动数据库服务的命令如下·: #service po ...
- 191016 Linux中安装python3
注意事项:直接在Linux系统中安装python3后会导致yum命令和pip命令失效. 安装python3过程(按下述方法安装依赖包.指定软链接,就不会出错了): # 安装依赖包 yum instal ...
- 分布式结构化存储系统-HBase应用案例
分布式结构化存储系统-HBase应用案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 为了让读者更进一步了解HBase在实际生成环境中的应用方法,在董西成的书里介绍两个经典的HB ...