最长子序列

最长子序列问题详解 提到最长子序列问题,想必大家都不陌生,今天我主要想分享一下我对子序列问题的一些理解: 先拿最长上升子序列问题来说吧: 很明显这是一个动态规化问题,仔细想想也不难得出其状态转移方程 首先介绍一下dp[]数组的含义:dp[i]表示以第i个数结尾的最长上升子序列长度,接下来就是状态转移的具体方法了 就是每更新到i时,就用dp[1~i-1]更新dp[i]的值,当j<i且a[j]<a[i]时,dp[i]=max(dp[i],dp[j]+1),这样我们就用n^2的复杂度处理完了这个问题 下面给出一 阅读详情

1、动态规划解法

两个关键:

①、无后效性的状态;

②、状态转移方程

#include<cstdio> 
#include<algorithm>
using namespace std;

const int maxn=10010;
int A[maxn];
int dp[maxn];

int main()
{
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		scanf("%d",&A[i]);
	}
	
	//边界
	dp[0]=A[0];
	for(int i=1;i<n;i++)
	{
		//状态转移方程 
		dp[i]=max(A[i],A[i]+dp[i-1]);
	} 
	
	int k=0;  //存放下标 
	for(int i=1;i<n;i++) 
	{
		if(dp[i]>dp[k])
		{
			k=i;
		}
	}
	printf("%d\n",dp[k]);
	return 0;
}

2、暴力解法

#include<cstdio>
using namespace std;

int a[6]={-2,11,-4,13,-5,-2};

int sum(int i,int j)
{
	int ans=0;
	for(int k=i;k<=j;k++)
	{
		ans+=a[k];
	}
	return ans;
} 

int main()
{
	int max=-2;
	for(int i=0;i<6;i++)
	{
		for(int j=i;j<6;j++)
		{
			int s=sum(i,j);
			if(s>max) max=s;
		}
	}
	printf("%d",max);
	return 0;
}

 

关于寻找两个字符串中最长子序列的问题 首先先来介绍一下最长子序列是什么 按照我的理解,最大子序列首先是两个串中相同的字符组成的,在两个字符串中,相同的字符可以不连续,但是必须字符的下标在字符串中是一次递增的。 举个简单的例子: String “abdcegs”; String “wbcasq”; 在这两个串中子序列有“as”,“bcs‘’ 最大子序列为“bcs”这种题我一共遇到两个解法,现在分享出来第一种解法:递归解决解法 阅读详情

相关推荐

(一篇即成功)Ubuntu下搭建qt环境和安装Qt Creator

只需一篇就成功搭建Ubuntu下的QT环境 你还在等什么

m0_73450461的博客 1万+

最长公共子序列(LCS) 过程图解

1.基本概念 首先需要科普一下,最长公共子序列(longest common sequence)和最长公共子串(longest common substring)不是一回事儿。什么是子序列呢?即一个给定的序列的子序列,就是将给定序列中零个或多个元素去掉之后得到的结果。什么是子串呢?给定串中任意个连续的字符组成的子序列称为该串的子串。给一个图再解释一下: 如上图,给定的字符序列: {a,b,c,d,e,f,g,h},它的子序列示例: {a,c,e,f} 即元素b,d,g,h被...

9201

通达信源码-主力建仓-AI投研图灵室出品

AI投研图灵室1:=3*SMA((CLOSE-LLV(LOW,75))/(HHV(HIGH,75)-LLV(LOW,75))*100,20,1)-2*SMA(SMA((CLOSE-LLV(LOW,75))/(HHV(HIGH,75)-LLV(LOW,75))*100,20,1),15,1);AI投研图灵室2W:=100-100*(HHV(HIGH,14)-CLOSE)/(HHV(HIGH,14)-LLV(LOW,14));AI投研图灵室3:=SMA(SMA(AI投研图灵室2,3,1),3,1);

qq_57222058的博客 4391

最长公共子序列LCS(动态规划)—详解

一.基本概念 1、 子序列(subsequence): 一个特定序列的子序列就是将给定序列中零个或多个元素去掉后得到的结果(不改变元素间相对次序)。例如序列<A,B,C,B,D,A,B><A,B,C,B,D,A,B>的子序列有:<A,B><A,B>、<B,C,A><B,C,A>、<A,B,C,D,A><A,B...

weixin_44023658的博客 1万+

如何利用ADS中的耦合线设计带通滤波器并转换为HFSS模型

耦合线带通滤波器是一种常用的微带滤波器形式,由多个长度为四分之一波长的耦合线节组成,在所需频点形成谐振,构成滤波器的频率响应特性。首先我们先来了解一下耦合线带通滤波器的电路结构,见下图:包括两对耦合线,中间为公共连接点,其中两对耦合线的偶奇模阻抗为(Z1e,Z1o)和(Z2e,Z2o,端口输入阻抗为Z0)。2.指定一个初始奇偶模值,ADS优化出一个合适的结果,通过奇偶模值利用linecalc工具计算出微带耦合线尺寸。假设中心频率为2GHz,则对应频点的电长度为。5.运行结果,两个软件仿真结果基本一致。

auqihc的博客 2230

嵌入式开发避坑指南:手把手教你用STM32CubeMX配置W9825G6KH SDRAM(含时序参数详解)

本文详细介绍了如何使用STM32CubeMX配置W9825G6KH SDRAM,包括硬件连接检查、CubeMX初始化步骤、时序参数优化及稳定性问题排查。通过实测波形分析和案例分享,帮助开发者避免常见配置错误,提升嵌入式系统性能与稳定性。

weixin_42542669的博客 197

动态规划——最长子序列、子串问题

动态规划——最长(连续)子序列问题0 未完成,待续1. 概述2. 最长子序列(不要求连续)3. 最长连续子序列 0 未完成,待续 1. 概述 其实本问题有两大类,一类是要求连续的,一类是要求不连续的。 先来看子序列与子串的区别,已知原数组为 1, 2, 4, 3, 7, 6, 5] ,则: [1, 2, 4] 为原数组的子串,三个元素收尾相连,中间没有别的元素; [1, 3, 5] 为子序列,各个元素之间可能隔着别的元素。 2. 最长子序列(不要求连续) 3. 最长连续子序列 一维数组,动规数组定义为

chuangshangbeidong的博客 2798

VOS3000线路对接、路由配置与路由分析操作教程

通过本教程,你应该已经掌握了VOS3000的线路对接、路由配置和路由分析的基本操作。添加备用路由:为常用前缀添加备用路由,确保在主路由失效时呼叫仍能正常进行。进入线路管理:点击“系统管理” -> “线路管理” -> “添加线路”。查看日志:在“系统日志”中查看路由选择情况,确保路由配置正确。失败原因:查看失败呼叫的原因(如“无应答”、“线路忙”等)。线路选择:选择之前添加的线路(如“供应商A”)。保存并启用:填写完毕后,点击“保存”并启用路由。路由名称:自定义一个名称(如“路由A”)。

AI_wx_3307623172的博客 2975

算法3-------最长子序列和

题目: 给定一个序列(至少含有 1 个数),从该序列中寻找一个连续的子序列,使得子序列的和最大。 例如,给定序列[-2,1,-3,4,-1,2,1,-5,4], 连续子序列[4,-1,2,1] 的和最大,为6。 思路1:贪心法【时间复杂度为O(n2)】 设置两个最大标记:subSum = max( 当前值,之前的序列+当前值 )    ...

weixin_34320724的博客 240

[Dify实战] 用 Dify 打造高效的新员工 7 天成长计划-带绑定资源

本文介绍了如何利用Dify平台为新员工制定智能成长计划,解决融入难、节奏乱、成长慢等问题。方案通过输入岗位信息自动生成7天结构化计划,包含每日任务和评估要点,并支持进度跟踪与报告输出。关键点在于设计清晰的Prompt模板,合理分配学习节奏,形成闭环反馈机制。该方案能显著提升新员工培养效率,实现人才培养的数字化升级。

技术小甜甜:传播可落地技术,帮助个人和企业降低解决真实场景问题的成本 570

DeerFlow 系列教程 第十一篇 | Sandbox 沙箱与 Memory 记忆——安全执行与持久认知

用户请求│▼Lead Agent├── 系统提示词组装│ ├── <memory> 段 ← Memory 系统注入用户记忆│ ├── <skill_system> 段│ └── <subagent_system> 段│├── 工具调用│ ├── read_file ├── Sandbox 系统提供隔离执行│ ├── write_file├── 路径虚拟化 + 安全验证│└── 对话结束│▼│├── 过滤消息(去除工具调用中间步骤)├── 检测纠正/强化信号。

weixin_43674738的博客 163

动态规划之最长子序列和最长子串

本文大部分参考July博客的关于动态规划的分析,代码贴在此仅作记忆和分享。   概念解释:   1) 最长子序列,两个字符串中满足相同顺序的子字符序列,如 abefgs和acehfg的最长子序列为aefg    2)   最长子串,两个字符串中相同的子串,即必须是连续相同

datiancai2008的专栏 1270

【工具篇】万字长文拆解 Otter.ai:语音转文字领域的「瑞士军刀」是如何炼成的?(附保姆级教程)

但需要注意,2024年欧盟已出台《AI转录伦理指南》,要求所有录音需获得参与者明确授权。举个真实案例:某硅谷创业团队用Otter.ai记录产品评审会,系统自动标红争议点并生成待办事项,会议效率提升40%。AIGC技术原理、 实时转录解析、 定价策略分析、 语音算法拆解、 多平台兼容测试、 深度学习框架、 空间计算专利。——无论是跨国会议、课堂讲座还是深夜灵感,它都能实时生成带时间戳的文本记录,准确率高达95%。(此处建议插入Otter.ai会议转录动图,展示实时字幕生成效果)(数据来源:综合参考文献整理)

商务合作|问题讨论|交流学习 请联系作者微信,加微信请务必注明来意,博客主页有联系方式 1260

数组:最长子序列问题四种解法

数组:最长子序列问题四种解法 问题描述:   给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。 示例 1 : 输入: [-2,1,-3,4,-1,2,1,-5,4], 输出: 6 解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。 解法一、三重for循环 解题思路: 通过前两层for循环找出数组的所有组合,第三层for循环计算该组合的sum,如果大于max,则赋值给max。   时间复杂度是O(N3),空间复杂度是O(1)。 public

Milan_1in的博客 3846

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

最长子序列的长度

最长子序列问题(two ways) 复杂度为O(n^2) 朴素算法, 代码如下: #include&amp;amp;amp;amp;lt;cstdio&amp;amp;amp;amp;gt;//或者用bits万能库 using namespace std; const int MAX=1001; int a[MAX]; int lis(int x) { int num[MAX]; for(int i=0;i&amp;amp;

qq_34771124的博客 1115

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

最长子序列和公共序列

1.最长子序列 比如:给定几个数,求最大的和,那么就是求他的最长子序列,那么可以通过一边输入一边求最大和的方法进行求解 for(i=1;i&lt;=N;i++) { cin&gt;&gt;a[i]; t[i]=max(t[i-1]+a[i],a[i]); } 例题:Max Sum http://acm.h...

weixin_42804250的博客 452

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

最长子序列和

Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array[−2,1,−3,4,−1,2,1,−5,4], the contiguous subarray...

走走停停的专栏 1015

2014. 重复 K 次的最长子序列

2014. 重复 K 次的最长子序列 (暴力) 注意到n<k×8n<k \times 8n<k×8 也就说满足条件的字符数不超过nk<8\dfrac{n}{k}<8kn​<8。 然后我们可以暴力长度为1到7的字符,每次找长度为iii的字符时我们可以根据i−1i-1i−1的字符进行递推这样可以节省时间,然后枚举字符的时候我们只需要枚举哪些出现次数大于等于kkk的字符。 最后输出的时候从长度大到小,然后优先输出末尾的,因为我们暴力找的时候是按照字典序从小到大进行的。 时间复杂

Harris-H的博客 619

K:找寻最长子序列的四个算法

package other; /** * 该类用于演示寻找最长子序列的几种算法 * @author 学徒 * */ public class MaxSubLined { public static void main(String[] args) { int[] array={-2,11,-4,13,-5,2,-5,-3,12,-9}; MaxSubLined max=new

MyStringIsNotNull的博客 597

最长子序列和子串相关算法题总结

文章目录最长子序列和子串相关算法题总结子串与子序列区别关键名词算法题1. LeetCode 674 : 最长连续递增序列2. LeetCode 673 : 最长递增子序列的个数3. LeetCode 300 : 最长上升子序列4. LeetCode 128 : 最长连续序列5. LeetCode 1143 : 最长公共子序列总结 最长子序列和子串相关算法题总结 算法题中经常碰见给定数组或者字符串求其相关的最长“连续递增”,“连续”等子序列或者子串。其主要是利用动态规划算法或者哈希表等数据结构来解决问题。 子

guihunkun的博客 492

最长子序列&最长子串的题型汇总

最长子序列和最长字串的题型汇总

weixin_42956047的博客 218

LeetCode——最长子序列和

找出给定数组中最长子序列的和 // 先算出每 func Max(x, y int)int{ if x<y{ return x } return y } func MaxSubSum(arr []int, n int) int { res := make([]int, n) res[0] = arr[0] maxs := arr[0] for i := 1; i < n...

李豆豆的博客 1158
上一篇: 动态规划
下一篇: 最长不下降子序列
Estrellas_
博客等级 码龄9年 273粉丝 233原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值