【PTA-A】1045 Favorite Color Stripe (30分)(LIS与LCS)

PTA最长对称子串(C语言版) 7-6 最长对称子串 (25分) 对给定的字符串,本题要求你输出最长对称子串的长度。例如,给定Is PAT&TAP symmetric?,最长对称子串为s PAT&TAP s,于是你应该输出11。 输入格式: 输入在一行中给出长度不超过1000的非空字符串。 输出格式: 在一行中输出最长对称子串的长度。 输入样例: Is PAT&TAP symmetric? 输出样例: 11 立即下载

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 (≤10​4​​) which is the length of the given stripe, followed by L colors on the stripe. All the numbers in a line a 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

 LIS和LCS两种做法都可以。

1、LIS(最大不下降)

#include<iostream>
#include<algorithm>
using namespace std;
const int N = 10010;
int a[N];
int ans[N],dp[N];
int main() {
	int n,m, t;
	cin >> n >>  m;
	int x;
	for (int i = 1; i <= m; i++) {
		cin >> x;
		a[x] = i;
	}
	int num = 0; cin >> t;
	for (int i = 0; i < t; i++) {
		cin >> x;
		if (a[x] >= 1) {
			ans[num++] = a[x];
		}
	}
	for (int i = 0; i < num; i++) {
		dp[i] = 1;
		for (int j = 0; j < i; j++) {
			if (ans[i] >= ans[j]) {
				dp[i] = max(dp[i], dp[j] + 1);
			}
		}
	}
	cout << dp[num-1];
	return 0;
}

2、LCS(最长公共和)

#include<iostream>
#include<algorithm>
using namespace std;
const int N = 10010;
int a[N], b[N], dp[N][N];
int main() {
	int n, m, t;
	cin >> n >> m;
	for (int i = 1; i <= m; i++) {
		cin >> a[i];
	}
	cin >> t;
	for (int i = 1; i <= t; i++) {
		cin >> b[i];
	}
	for (int i = 1; i <= m; i++) {
		for (int j = 1; j <= t; j++) {
			int MAX = max(dp[i][j - 1], dp[i - 1][j]);
			dp[i][j] = a[i] == b[j] ? (MAX + 1) : MAX;
		}
	}
	cout << dp[m][t];
	return 0;
}

 

c++最长公共子序列问题LCSLength 动态规划的问题解题思路都差不多(一些浅见),基本要素是最优子结构性质,子问题重叠性质,自底向上的求解方法。只要了解了基本要素,那么这种题型也会更好理解。本题有不少注释,便于读者阅读。 立即下载

相关推荐

求最长的单词

在一对字符串中截出你想要的字符串或单词,很有用

程序设计基础33 最长不下降子序列

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

qq2285580599的博客 362

世界第一的编程代码 看看吧 嘿嘿

点击运行一下 放心不会弄坏你的机子 很好玩儿的

【PTA】刷题总结(持续更新)

好像优快云目前不支持代码收缩的功能,页面有拖沓,可以根据目录来寻找位置。或者之后我会分个不同板块 目录 一、考点 二、代码小知识点 三、算法模板 1、堆排序 2、树的排序 3、LCA 四、例题 一、考点 1、排序:堆排序、插入排序 2、图论:拓扑图、最短路径(基本dijkstra)、遍历(深搜广搜) 3、树:前中后层遍历、前中求后/后中求前(前后求中【不唯一】)、完全二叉...

Netceor的博客 1212

找出所有最长公共子序列算法代码

所有最长公共子序列(LCS)——动态规划——Java---所有!!!所有!!!所有!!!

Elasticsearch 8.17.1 JAVA工具类

整理了Elasticsearch8.17最新版的API工具类

weixin_39555954的博客 1123

DP_最长不下降子序列(LIS)

序言 同类的问题还有“最长上升子序列”, “最长下降子序列”, … 他们的不同就在于定义的core规则不同, 有的是&amp;gt;=, 有的是&amp;gt;, 有的是&amp;lt; 由此启发, 我们可以在解决其他的问题, 不一定是比较数的大小的问题里面抽象出这种模型. 下面介绍这种动态规划入门都会介绍的问题的思路. 首先我们从头开始分析这个问题. 一. 最容易想到的最暴力的方法 对这个序列中的每...

姬小野的博客 9172

【拯救C盘】一文搞懂Windows修改Anaconda默认的虚拟环境存放路径及其原理

Anaconda创建的虚拟环境可能会默认保存在C盘,但C盘的存储空间很宝贵,由于每个虚拟环境的大小可能达到n个GB,稍不注意就会遇到C盘红色警告。因此有必要做点什么来拯救被虚拟环境占用的大部分空间!!!

Roach007的博客 3万+

pat甲级 动态规划 A1045 Favorite Color Stripe (30分)

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

weixin_42358356的博客 166

rk3588s-摄像头适配gc8034

若还打开失败,需要看下XML中文件配置是否与DTS配置信息一致,或者XML参数配置是否有异常;MIPI摄像头是有上电时序要求,具体时序要查看sensor芯片对应的datasheet进行修改,如果时序不满足,sensor无法正常,此时用i2ctool去查看设备可能无法扫面到设备,即是i2c设备找不到。RK3588或者RK3588S有路MIPI-PHY,驱动开发工程师需要根据对应的硬件原图进行配置,以下会讲解分别使用MIPI-DPHY和MIPI-D/CPHY如何在DTS配置mipi摄像头的回环链路。

Yxx8066的博客 2199

用ESP32-S3+OV2640摄像头+ILI9488屏,DIY一个能联网的智能猫眼(附完整代码)

本文详细介绍了如何利用ESP32-S3微控制器搭配OV2640摄像头和ILI9488显示屏,DIY一个功能强大的智能猫眼系统。从硬件选型、电路设计到软件开发,提供了完整的解决方案和优化技巧,包括图像采集显示、WiFi远程访问及低功耗设计,适合智能家居爱好者动手实践。

weixin_30897233的博客 499

【PAT】1045. Favorite Color Stripe

考查点:LIS或LCS 提交情况:段错误,这里是dp数组开成跟h数组一样了,应该与接受数组一样大,以后检查段错误除了开小了以外有可能数组所用最大值开错了  第一种是LIS版本: #define LOCAL #include #include #include #include #include #include #include #include #include #inc

天行健,君子以自强不息 490

U盘插入电脑时发出声音但不显示——编程解决方案

以上是一些常见的编程解决方案,帮助您解决U盘插入电脑时发出声音但不显示的问题。请注意,这些解决方案可能因不同的情况而略有不同,因此请根据您的具体情况进行调整和尝试。当您将U盘插入电脑时,如果您听到了连接声音,但是U盘却没有在文件资源管理器或桌面上显示出来,这可能是由于一些常见问题导致的。U盘的文件系统格式可能与您的计算机不兼容,导致无法正确识别。如果输出显示U盘的文件系统格式与您的计算机不兼容,您可以尝试重新格式化U盘或使用其他兼容的文件系统格式。其中,X是U盘的磁盘编号,Y是您想要分配给U盘的盘符。

ByteEchoX的博客 2461

PTA1045 Favorite Color Stripe(最长不降子序列,dp)

分析:现对给出来的喜欢的序列做一个hash,比如他喜欢的序列2 3 1 5 6,映射成0,1,2,3,4。然后通过最长不降子序列来解决。 dp[i]=max(dp[j]+1,1),其中满足0&lt;=j&lt;=idp[i]=max(dp[j]+1,1) ,其中满足0&lt;=j&lt;=idp[i]=max(dp[j]+1,1),其中满足0<=j<=i...

xx_ii 418

【AIGC】大模型面试高频考点07-注意力(Attention)篇

AIGC面试高频考点

一个内容严谨、方向前沿、极具实践价值的AI大模型技术分享站 3361

ImportError: DLL load failed: 找不到指定的程序的解决方案

本文主要介绍了ImportError: DLL load failed: 找不到指定的程序的解决方案,希望能对使用Python的同学们有所帮助。 文章目录 1. 问题描述 2. 解决方案

weixin_43178406的博客 18万+

PTA甲级 1045 Favorite Color Stripe (C++)

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

Misdirection 330

9、储备池学习:从学习到学习的新范式

本文探讨了基于学习到学习(L2L)框架的储备池计算新范式,提出通过在外循环中优化储备池的超参数(如递归连接权重、输入权重和读出初始化),以提升其在多种任务中的内循环学习性能。研究展示了两种主要实验设置:一是优化储备池用于回归二阶Volterra滤波器任务,显著优于随机基线;二是实现无需调整读出权重、仅依赖内部动态学习非线性函数的能力,表明储备池可快速构建目标函数的内部模型并安装任务先验知识。结合流程图与表格,文章系统呈现了L2L的训练流程、技术细节及实验结果,并讨论了性能提升、泛化能力等开放问题,指出了未来

ujm56789012的博客 500

有史以来最长的代码 第二周第一题

#include<cstdio> #include<cstdlib> #include<iomanip> #include<iostream> using namespace std; typedef struct node *link; typedef struct node { double a1; int b1; link next; }Node; link NewNode() { return (link)malloc(sizeof(Node)

olssa的博客 7336

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

坑爹代码|你写过的最长的一行代码有多长???

你写过的最长的一行代码有多长吗?你为什么要写这么长?是看着帅呢,还是想减少代码行数? List<OperationPurchaseInfo> purchaseInfoList = sheet.getPurchaseInfoList().stream().filter(purchaseInfo -> purchaseInfo.getExteriorOperation().get...

ZicoChan的博客 1448

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

【PAT甲级】1045 Favorite Color Stripe(30 分)(LCS转化为LIS)

题目链接 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...

feng_zhiyu的博客 567

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

1045 Favorite Color Stripe(30 分

1045 Favorite Color Stripe(30 分 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...

chenyutingdaima的博客 516

1045. Favorite Color Stripe (30)-PAT甲级真题

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

柳婼 の blog 3850

C++最长公共前缀完整代码

C++最长公共前缀完整代码

qianshu1212的博客 1722

【PAT 1045】 Favorite Color Stripe 最长公共子序列LCS

1045. Favorite Color Stripe (30) 时间限制 200 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Eva is trying to make her own color

天堂人间 6022

DP-最长不下降子序列 LIS

#include &lt;cstdio&gt; #include &lt;algorithm&gt; using namespace std; const int MAXN = 4000; int main() { int n, number[MAXN], dp[MAXN]; //dp[i]表示(强制)以A[i]结尾的最长不下降子序列的长度 scanf("%d",&amp;n...

仅做个人笔记 275
上一篇: (三)利用Express创建页面(包括express的安装和应用)
下一篇: R语言(一)——横截面数据回归
Netceor
博客等级 码龄9年 1046粉丝 306原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值