最长递增子序列 子串_最长递增子序列

最长递增子序列 子串

Description:

描述:

This is one of the most popular dynamic programming problems often used as building block to solve other problems.

这是最流行的动态编程问题之一,通常用作解决其他问题的基础。

Problem statement:

问题陈述:

Given a sequence A of size N, find the length of the longest increasing subsequence from the given sequence.

给定一个大小为N的序列A ,从给定序列中找到最长的递增子序列的长度

The longest increasing subsequence means to find a subsequence of a given sequence where the subsequence's elements are sorted in increasing order, and the subsequence is longest possible. This subsequence is not necessarily contiguous, or unique. Longest increasing subsequence is strictly increasing.

最长的增长子序列意味着找到给定序列的子序列,其中该子序列的元素按升序排序,并且该子序列可能最长。 该子序列不一定是连续的或唯一的。 最长增加的子序列严格增加。

    Input:
N=7
Sequence:
{2, 3, 4, 0, 1, 2, 3, 8, 6, 4}
Output:
Length of Longest increasing subsequence is 5
Longest increasing subsequence= {0, 1, 2, 3, 8} or {0, 1, 2, 3, 4}

Explanation with example

举例说明

The possible increasing sub-sequences are,

可能增加的子序列是

Of Length 1 //each element itself is an increasing sequence

长度为1 //每个元素本身都是递增序列

Longest Increasing Subsequence (1)
Longest Increasing Subsequence (2)
Longest Increasing Subsequence (3)
Longest Increasing Subsequence (4)
Longest Increasing Subsequence (5)

So, on...

所以...

Longest Increasing Subsequence (6)
Longest Increasing Subsequence (7)

So, on...

所以...

Longest Increasing Subsequence (8)
Longest Increasing Subsequence (9)

So, on...

所以...

Longest Increasing Subsequence (10)

No more
Of Length 6
None

不再
长度6
没有

So, the longest increasing subsequence length is 5.

因此,最长的递增子序列长度是5。

问题解决方法 (Problem Solution Approach)

Of course, in brute-force we can simply generate all increasing sequences and find the longest one. But it would take exponential time which is not a feasible solution. Hence, we choose Dynamic programming to solve.

当然,在蛮力作用下,我们可以简单地生成所有递增的序列并找到最长的序列。 但是,这将花费指数时间,这不是可行的解决方案。 因此,我们选择动态规划来解决。

We create a DP table to store longest increasing subsequence length.
It's intuitive that the minimum value would be 1 as each element represents the primitive sequence which is an increasing one.

我们创建一个DP表来存储最长的递增子序列长度。
直观的是,最小值将为1,因为每个元素代表原始序列,该序列是递增的。

So, the base value is 1.

因此,基准值为1。

Now,

现在,

    Lis(i) = longest increasing subsequence starting from index 0 to index i

So,
To compute L(i) the recursion function is,

所以,
为了计算L(i) ,递归函数为

Longest Increasing Subsequence (i)

As, the base value is 1, for every index i, L(i) is at least 1.

这样,对于每个索引i ,基值为1, L(i)至少为1。

    
1) Create the DP array, Lis[n]
2) Initialize the DP array.
for i=0 to n-1
lis[i]=1;
3) Now, to compute the Lis[i]
for index  i=1 to n-1         
for previous index j=0 to i-1
// if (arr[i],arr[j]) is inceasing sequence
if(lis[i]<lis[j]+1 && a[i]>a[j])
lis[i]=lis[j]+1;
end for
end for 

Initially DP table,

最初是DP表,

Longest Increasing Subsequence (11)
Longest Increasing Subsequence (12)

So, the maximum out of this is 5
Hence, LIS=5.

因此,最大数量为5
因此,LIS = 5。

C++ implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;
int max(int a, int b)
{
if (a > b)
return a;
else
return b;
}
int LIS(vector<int> a, int n)
{
int lis[n];
//base case
for (int i = 0; i < n; i++)
lis[i] = 1;
//fill up table
for (int i = 1; i < n; i++) {
for (int j = 0; j < i; j++) {
if (lis[i] < lis[j] + 1 && a[i] > a[j])
lis[i] = lis[j] + 1;
}
}
//return LIS
return *max_element(lis, lis + n);
}
int main()
{
int n, item;
cout << "Sequence size:\n";
scanf("%d", &n);
//input the array
vector<int> a;
cout << "Input sequence:\n";
for (int j = 0; j < n; j++) {
scanf("%d", &item);
a.push_back(item);
}
cout << "Length of longest incresing subsequence is: " << LIS(a, n) << endl;
return 0;
}

Output

输出量

Sequence size:
10
Input sequence:
2 3 4 0 1 2 3 8 6 4
Length of longest incresing subsequence is: 5

翻译自: https://www.includehelp.com/icp/longest-increasing-subsequence.aspx

最长递增子序列 子串

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/543913.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

WPF自定义控件与样式(5)-Calendar/DatePicker日期控件自定义样式及扩展

原文:WPF自定义控件与样式(5)-Calendar/DatePicker日期控件自定义样式及扩展一&#xff0e;前言 申明&#xff1a;WPF自定义控件与样式是一个系列文章&#xff0c;前后是有些关联的&#xff0c;但大多是按照由简到繁的顺序逐步发布的等&#xff0c;若有不明白的地方可以参考本…

bba70_BBA的完整形式是什么?

bba70BBA&#xff1a;工商管理学士 (BBA: Bachelor of Business Administration) BBA is an abbreviation of Bachelor of Business Administration also spelled as B.B.A. In the field of Business Administration, it is an undergraduate degree program. This is a degre…

Qt和纹理

2019独角兽企业重金招聘Python工程师标准>>> test 转载于:https://my.oschina.net/assange/blog/537631

计算机图形学图形旋转_计算机图形学翻译

计算机图形学图形旋转计算机图形学| 翻译 (Computer Graphics | Translations) Transformation techniques mean to modify the current shape or object in a particular manner. Changing of an object after creation, in terms of position or even size is known as trans…

AP 1532E register   Cisco 2504 AP注册WLC

客户的环境是&#xff1a;WLC是 2504 的AP的型号是 1532E的首先要是版本匹配&#xff0c;那么我们就要查一个兼容性列表&#xff0c;请看附件同时&#xff0c;我们要把WLC的版本升级到AIR-CT2500-K9-8-1-111-0.aes 这个版本&#xff1b;同时由于瘦AP 1532E的版本是 Cisco IOS S…

C#中IDisposable 回收非托管资源

C#中IDisposable 更多2014/9/7 来源&#xff1a;C#学习浏览量&#xff1a;4185学习标签&#xff1a; IDisposable本文导读&#xff1a;C#中IDisposable接口的主要用途是释放非托管资源。当不再使用托管对象时&#xff0c;垃圾回收器会自动释放分配给该对象的内存。但无法预测进…

css导航栏_使用CSS的导航栏

css导航栏CSS | 导航栏 (CSS | Navigation Bar) Developing websites is great but developing a user-friendly website is even greater. So how does one design a user-friendly website? What tools to use? Well, there are many tools to mention which are quite hel…

Java代理系列-静态代理

2019独角兽企业重金招聘Python工程师标准>>> 代理模式可以做很多事&#xff0c;像hibernate&#xff0c;spring都使用了代理模式。 spring的aop就是用代理做的。 本系列分为4章&#xff0c;静态代理&#xff0c;动态代理热身&#xff0c;动态代理&#xff0c;cglib代…

ajax的模式_AJAX的完整形式是什么?

ajax的模式AJAX&#xff1a;异步JavaScript和XML (AJAX: Asynchronous JavaScript and XML) AJAX is an abbreviation of Asynchronous JavaScript and XML. It is an organized collection of technologies and not of a single technology. Informing a collection of web De…

JAVA Opencv在图片上添加中文

问题描述&#xff1a; 将图片进行均值、中值、高斯滤波&#xff0c;高斯边缘检测&#xff0c;并在图片上添加中文文字。 一、算法思想 首先经过opencv的一系列操作&#xff0c;例如高斯模糊、均值模糊等操作后、用Imgcodecs.imwrite方法将图片写出到指定的位置。再利用java…

双向tvs和单向tvs_TVS的完整形式是什么?

双向tvs和单向tvsTVS&#xff1a;Thirukkurungudi Vengaram Sundram (TVS: Thirukkurungudi Vengaram Sundram) TVS is an abbreviation of Thirukkurungudi Vengaram Sundram. It is a multinational motorcycle business corporation, which is one of the largest manufactu…

引用头文件报错 .pch引用不了其他的.h文件

2019独角兽企业重金招聘Python工程师标准>>> 一、编绎显示Unknown type name “CGFloat” 错误解决方法 将Compile Sources As 改为 Objective-C 二、如果是extern const引起的。直接加头文件 #import <UIKit/UIKit.h> 最后在 .h文件 #import <UIKit/UIK…

ibm mq的交互命令模式_IBM的完整形式是什么?

ibm mq的交互命令模式IBM&#xff1a;国际商业机器 (IBM: International Business Machines) IBM is an abbreviation of International Business Machines. It is an I.T based multinational and consulting corporation which is also an American trusted brand in the IT …

r软件说明lib文件未指明_软件说明文件

r软件说明lib文件未指明The software primarily consists of Computer Programs and the associated documentation. We all know that the computer program is the baseline of the entire software, but the documentation part is also as important as the programming pa…

dfa与ndfa_DFA和NDFA之间的区别| 目录

dfa与ndfaDFA stands for Deterministic Finite Automata and NDFA stands for Non-Deterministic Finite Automata. DFA代表确定性有限自动机&#xff0c;而NDFA代表非确定性有限自动机。 Read more: Deterministic Finite Automata (DFA) 阅读更多&#xff1a; 确定性有限自…

css圆角三角形3个圆角_CSS中的圆角

css圆角三角形3个圆角CSS | 圆角 (CSS | Rounded Corners) border-radius property is commonly used to convert box elements into circles. We can convert box elements into the circle element by setting the border-radius to half of the length of a square element.…

iti axi dsp_ITI的完整形式是什么?

iti axi dspITI&#xff1a;工业培训学院 (ITI: Industrial Training Institute) ITI is an abbreviation of the Industrial Training Institute. It offers training in engineering and non-engineering technical fields. It is a post-secondary school in India which is…

appweb ejs_具有快速路线的EJS

appweb ejsHI! Welcome to NODE AND EJS TEMPLATE ENGINE SERIES. Today, we will see how we can work with EJS and routes? 嗨&#xff01; 欢迎使用NODE和EJS模板引擎系列 。 今天&#xff0c;我们将看到如何使用EJS和路由&#xff1f; A route is like a sub domain wit…

ups一直响是什么原因_UPS的完整形式是什么?

ups一直响是什么原因UPS&#xff1a;不间断电源 (UPS: Uninterruptible Power Supply) UPS is an abbreviation of Uninterruptible Power Supply. It operates with the support of a battery which is used to supply power in the lack of most important source or when th…

语音asr是什么意思_ASR的完整形式是什么?

语音asr是什么意思ASR&#xff1a;自动语音识别 (ASR: Automated Speech Recognition) ASR stands for Automated Speech Recognition. With the help of this technology, spoken words can be easily converted to written text. What actually it does? It gives access to…