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. ...
随机推荐
- System.ComponentModel.Win32Exception (0x80004005): 无效的窗口句柄。
原文:System.ComponentModel.Win32Exception (0x80004005): 无效的窗口句柄. 在 WPF 获取鼠标当前坐标的时候,可能会得到一个异常:System.Co ...
- Core 导出(流和URL两种)
1.流 2.URL 两种都是使用Epplus 1. EPPlus的基本介绍 EPPlus是一个使用Open Office XML(xlsx)文件格式,能读写Excel 2007/2010 文件的开源组 ...
- 第三方dll签名
1.打开vs Tools下的工具命令 2.生成随机密钥对C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC>sn -k NonSignL ...
- Java字节流文件复制
1.字节流 在 Java 中,文件的复制使用字节输入流和字节输出流实现,java.io 包有 InputStream 和 OutputStream 这两个顶层抽象类规范了读写文件所需的核心 API. ...
- 设置body样式问题
如果我给body设置成一个宽高为200px的正方形,背景为红色,但是整个html也变成了红色,而且是整个浏览器屏幕都是红的,怎么来处理,如下 给html单独设置一个背景颜色,比如为白色#fff,在给b ...
- Nginx记录post body内容
nginx在记录http的body内容时,会将中文转义为16进制 在nginx 1.11.8 以上版本中log_format 增加了escape=json 参数,可以不转义变量内容: log_form ...
- 本地ssh快速登录
每次登录都要ssh -p wang@xx.xx.xx.xx 虽然做了公钥验证 https://www.cnblogs.com/php-linux/p/10795913.html 不需要输入密码,但是每 ...
- CRM-Modelformset
CRM-Modelformset 效果图 利用modelformset实现上面的效果: views.py文件: from user.formself.myform import RegForm, Cu ...
- oracle 设置归档日模式
首先关闭ORACLE SQL> shutdown immediate 把ORACLE启动为MOUNT模式 SQL:>startup mount sql:> alter databas ...
- 【转】常见的tcp/ip协议的知识
1.tcp/ip协议的层数 左图是osi 7层模型,右图是tcp/ip 4层模型.二者对应关系如上. 2.socket中TCP的三次握手建立连接详解 我们知道tcp建立连接要进行“三次握手”,即交换三 ...