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)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  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. 【PAT甲级】1045 Favorite Color Stripe (30 分)(DP)

    题意: 输入一个正整数N(<=200),代表颜色总数,接下来输入一个正整数M(<=200),代表喜爱的颜色数量,接着输入M个正整数表示喜爱颜色的编号(同一颜色不会出现两次),接下来输入一个 ...

  7. PAT (Advanced Level) 1045. Favorite Color Stripe (30)

    最长公共子序列变形. #include<iostream> #include<cstring> #include<cmath> #include<algori ...

  8. 1045 Favorite Color Stripe 动态规划

    1045 Favorite Color Stripe 1045. Favorite Color Stripe (30)Eva is trying to make her own color strip ...

  9. 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. ...

随机推荐

  1. C# VS启动调试项目允许外网调试(微信开发)

    转发链接:https://blog.csdn.net/sinat_23050697/article/details/62889693 主要效果是本机调试网站,将网站发布到某域名(如m16758r728 ...

  2. python3基础之“小练习(1)”

    (一)打印3个不同的字符 # a=int("123") # b="123" # c=1.2 # print(type(a),a) # print(type(b) ...

  3. SIM7000C TCP

    SIM7000C是基于高通MDM9206平台开发LTE CAT M1(eMTC) 和NB-IoT模块,能支持LTETDD/LTE-FDD/GSM/GPRS/EDGE多个频段,上下行数据流量峰值达到37 ...

  4. 那些年伴我一起成长的SAP装备

    今天这篇文章无关技术,我们来聊聊SAP装备,即打上了SAP logo的那些物品. 但凡在SAP圈子工作过一段时间的从业者们,手上或多或少都拥有一些此类装备.Jerry当然也不例外,这些装备无论物品本身 ...

  5. 大数据的前世今生【Hadoop、Spark】

      一.大数据简介 大数据是一个很热门的话题,但它是什么时候开始兴起的呢? 大数据[big data]这个词最早在UNIX用户协会的会议上被使用,来自SGI公司的科学家在其文章“大数据与下一代基础架构 ...

  6. [LeetCode] 5. 最长回文子串 ☆☆☆(最长子串、动态规划)

    最长回文子串 (动态规划法.中心扩展算法) https://leetcode-cn.com/problems/longest-palindromic-substring/solution/xiang- ...

  7. 用D3.js画树状图

    做项目遇到一个需求,将具有层级关系的词语用树状图的形式展示它们之间的关系,像这样: 或者是这样: 上面的图片只是样例,跟我下面的代码里面用的数据不同 网上有很多这种数据可视化展示的js控件,我这里选择 ...

  8. MySQL Binlog--PURGE MASTER LOGS失败

    问题背景: 在我们磁盘空间维护策略中,BINLOG的默认保留期限为7天,但当磁盘空间不足时,会根据磁盘空间使用率自动清理超过一定数量的BINLOG. 问题原因: 某服务器上报磁盘空间不足,登录服务器发 ...

  9. 臀部——哑铃&杠铃

  10. SQL SERVER-Alwayson原理

    流程 1.异步提交模式 主副本无须确认该副本已经完成日志固化,就可提交事务. 主副本不受辅助副本的影响 辅助副本上的DB处于SYNCHRONIZING 2.同步提交模式 主副本要确认副本已经完成日志固 ...