日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2025/3/11 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 最长递增子序列 子串_最长递增子序列 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

最長(zhǎng)遞增子序列 子串

Description:

描述:

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

這是最流行的動(dòng)態(tài)編程問(wèn)題之一,通常用作解決其他問(wèn)題的基礎(chǔ)。

Problem statement:

問(wèn)題陳述:

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

給定一個(gè)大小為N的序列A ,從給定序列中找到最長(zhǎng)的遞增子序列的長(zhǎng)度

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.

最長(zhǎng)的增長(zhǎng)子序列意味著找到給定序列的子序列,其中該子序列的元素按升序排序,并且該子序列可能最長(zhǎng)。 該子序列不一定是連續(xù)的或唯一的。 最長(zhǎng)增加的子序列嚴(yán)格增加。

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

Explanation with example

舉例說(shuō)明

The possible increasing sub-sequences are,

可能增加的子序列是

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

長(zhǎng)度為1 //每個(gè)元素本身都是遞增序列

So, on...

所以...

So, on...

所以...

So, on...

所以...

No more
Of Length 6
None

不再
長(zhǎng)度6
沒(méi)有

So, the longest increasing subsequence length is 5.

因此,最長(zhǎng)的遞增子序列長(zhǎng)度是5。

問(wèn)題解決方法 (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.

當(dāng)然,在蠻力作用下,我們可以簡(jiǎn)單地生成所有遞增的序列并找到最長(zhǎng)的序列。 但是,這將花費(fèi)指數(shù)時(shí)間,這不是可行的解決方案。 因此,我們選擇動(dòng)態(tài)規(guī)劃來(lái)解決。

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.

我們創(chuàng)建一個(gè)DP表來(lái)存儲(chǔ)最長(zhǎng)的遞增子序列長(zhǎng)度。
直觀的是,最小值將為1,因?yàn)槊總€(gè)元素代表原始序列,該序列是遞增的。

So, the base value is 1.

因此,基準(zhǔn)值為1。

Now,

現(xiàn)在,

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

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

所以,
為了計(jì)算L(i) ,遞歸函數(shù)為

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

這樣,對(duì)于每個(gè)索引i ,基值為1, L(i)至少為1。

1) Create the DP array, Lis[n]2) Initialize the DP array.for i=0 to n-1lis[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 sequenceif(lis[i]<lis[j]+1 && a[i]>a[j])lis[i]=lis[j]+1;end forend for

Initially DP table,

最初是DP表,

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

因此,最大數(shù)量為5
因此,LIS = 5。

C++ implementation:

C ++實(shí)現(xiàn):

#include <bits/stdc++.h> using namespace std;int max(int a, int b) {if (a > b)return a;elsereturn b; }int LIS(vector<int> a, int n) {int lis[n];//base casefor (int i = 0; i < n; i++)lis[i] = 1;//fill up tablefor (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 LISreturn *max_element(lis, lis + n); } int main() {int n, item;cout << "Sequence size:\n";scanf("%d", &n);//input the arrayvector<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

最長(zhǎng)遞增子序列 子串

總結(jié)

以上是生活随笔為你收集整理的最长递增子序列 子串_最长递增子序列的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。