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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

最长公共子序列求序列模板提_最长公共子序列

發布時間:2023/12/1 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 最长公共子序列求序列模板提_最长公共子序列 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最長公共子序列求序列模板提

Description:

描述:

This question has been featured in interview rounds of Amazon, MakeMyTrip, VMWare etc.

這個問題在亞馬遜,MakeMyTrip,VMWare等訪談輪次中都有介紹。

Problem statement:

問題陳述:

Given two strings str1 and str2, find length of the longest common sub-sequence between them

給定兩個字符串str1str2 ,找到它們之間最長的公共子序列的長度

Let the strings be str1="includehelp"str2="letsinclude"Output will be:Longest common sub-sequence length is 7The longest common sub-sequence is: "include"

The output is given above where the longest common sub-sequences is in same colour.

上面給出了最長公共子序列為相同顏色的輸出。

Solution Approach:

解決方法:

The problem can be solved in a brute-force way. By generating all sub-sequences and checking them whether equal or not. Finally taking the longest common subsequence. But undoubtedly this is not at all computable since generating all sub-sequence is itself exponential and then permutations for checking any two sub-sequences.

這個問題可以用蠻力解決。 通過生成所有子序列并檢查它們是否相等。 最后取最長的公共子序列。 但是毫無疑問,這根本不是可計算的,因為生成所有子序列本身就是指數的,然后進行排列以檢查任意兩個子序列。

The recursive way to solve is

遞歸的解決方法是

Let,

讓,

l1 = Length of the first string,str1l2 = Length of the second string,str2f(l1,l2) = Longest common subsequence length for string lengths l1 & l2

Now,

現在,

Think of the following example,

考慮以下示例,

Say first string is: x1 x2 ... xl1

假設第一個字符串是: x 1 x 2 ... x l 1

And the second string is: y1 y2 ... yl2

第二個字符串是: y 1 y 2 ... y l 2

Say,

說,

Then obviously we need to find LCS for the remaining part of string

and then add 1 for this character match

那么顯然我們需要為字符串的其余部分找到LCS

Else

其他

Maximum of two case

最多兩種情況

  • LCS of the first string leaving character

    and second string

    第一個字符串離開字符的LCS

    和第二個字符串
  • LCS of the first string

    and second string leaving character

    第一個字符串的LCS

    和第二個字符串離開字符
  • Now, we need to recur down to 0. So,

    現在,我們需要遞歸降至0。因此,

    Where base cases are,

    在基本情況下,

    If you generate this recursion tree, it will generate many overlapping sub-problems and thus, we need to reduce the re-computing. That’s why we need to convert it into dynamic programming where we will store the output of the sub-problems and we will use it to compute bigger sub-problems.

    如果生成此遞歸樹,它將生成許多重疊的子問題,因此,我們需要減少重新計算。 這就是為什么我們需要將其轉換為動態編程,以便在其中存儲子問題的輸出,并使用它來計算更大的子問題。

    轉換為動態編程 (Converting to Dynamic programing)

    1) Initialize dp[l1+1][l2+1] to 02) Convert the base case of recursion:for i=0 to l1dp[i][0]=0;for i=0 to l2dp[0][i]=0;3) Fill the DP table as per recursion.for i=1 to l1 //i be the subproblem length for str1for j=1 to l2 //j be the subproblem length for str2if(str1[i-1]==str2[j-1]) //xl1==yl2dp[i][j]=dp[i-1][j-1]+1;elsedp[i][j]=max(dp[i-1][j],dp[i][j-1]);end forend for 4) The final output will be dp[l1][l2]

    C++ Implementation:

    C ++實現:

    #include <bits/stdc++.h> using namespace std;int max(int a, int b) {return (a > b) ? a : b; }int LCS(string str1, string str2) {int l1 = str1.length();int l2 = str2.length();int dp[l1 + 1][l2 + 1];for (int i = 0; i <= l1; i++)dp[i][0] = 0;for (int i = 0; i <= l2; i++)dp[0][i] = 0;for (int i = 1; i <= l1; i++) {for (int j = 1; j <= l2; j++) {if (str1[i - 1] == str2[j - 1])dp[i][j] = dp[i - 1][j - 1] + 1;elsedp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);}}return dp[l1][l2]; } int main() {string str1, str2;cout << "Enter first string\n";cin >> str1;cout << "Enter Second string\n";cin >> str2;cout << "Longest Common sub-sequence length is: " << LCS(str1, str2) << endl;return 0; }

    Output

    輸出量

    Enter first string includehelp Enter Second string letsincludeus Longest Common sub-sequence length is: 7

    翻譯自: https://www.includehelp.com/icp/longest-common-subsequence.aspx

    最長公共子序列求序列模板提

    總結

    以上是生活随笔為你收集整理的最长公共子序列求序列模板提_最长公共子序列的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

    主站蜘蛛池模板: 中文字幕被公侵犯的漂亮人妻 | 亚洲国产成人va在线观看天堂 | a级片黄色 | 中文字幕在线观 | 伊人91视频 | 性福宝av | 欧美一级爽aaaaa大片 | 三级视频在线 | 欧美一区免费观看 | 亚洲一区二区三区四区在线播放 | 操女人视频网站 | 亚洲午夜免费 | 国产精品果冻传媒 | 亚洲天堂网在线观看 | 丰满人妻一区二区三区性色 | 午夜精品视频在线 | 99爱视频在线 | 丁香在线视频 | 成人免费毛片果冻 | hd性videos意大利精品 | 久久综合亚洲 | 漂亮人妻被黑人久久精品 | 成人激情小视频 | 久久福利电影 | 黄色a级片视频 | 日韩av三级在线 | 欧美一级黄色片视频 | 色偷偷噜噜噜亚洲男人的天堂 | 亚洲国产一区二区三区a毛片 | 色呦呦中文字幕 | 福利免费在线观看 | 久久99热这里只频精品6学生 | www香蕉| 视色视频在线观看 | 久久久情| 国产精品男人的天堂 | av大片在线播放 | 久久久久亚洲av无码专区首jn | a级无毛片 | 成人av一区二区三区在线观看 | 国内一区二区 | 91久久精品夜夜躁日日躁欧美 | 国产精品第八页 | 夜夜成人 | 亚洲精品久久夜色撩人男男小说 | 成人午夜免费福利视频 | 国产在线精品视频 | 久久久人妻无码一区二区 | 欧美成人一区在线 | 国产一级黄色大片 | wwwxxx日本免费 | 亚洲AV无码AV吞精久久中文版 | 性欧美精品| 秋霞黄色片 | 久久天天躁狠狠躁夜夜躁2014 | 天堂资源在线观看 | 香蕉久久一区二区三区 | 狠狠爱综合网 | 国产激情成人 | 亚洲另类图区 | 色一情一区二区三区四区 | 亚洲乱码一区二区三区 | 精品理论片 | 影音先锋人妻啪啪av资源网站 | 青青草伊人久久 | 一级日韩一级欧美 | 美女扣逼喷水视频 | 国产无套内射又大又猛又粗又爽 | 国产夫绿帽单男3p精品视频 | 久久精品66 | 天堂成人在线 | 亚洲欧美一区二区在线观看 | 污污在线看 | 久久久免费网站 | 日本一区二区不卡视频 | 亚洲激情午夜 | 日本猛少妇色xxxxx猛叫 | 色在线播放 | 91久久综合亚洲鲁鲁五月天 | 美女视频免费在线观看 | 天天操天天干天天爱 | 日本精品视频一区二区三区 | 夜夜添无码一区二区三区 | 狠狠爱五月婷婷 | 99久久视频| 人人舔人人插 | 国产欧美精品久久久 | 精品久久免费观看 | 日本一区二区在线免费观看 | 中文字幕无码乱码人妻日韩精品 | 高h教授1v1h喂奶 | jzzjzz日本丰满少妇 | 国产精品第七页 | 亚洲欧洲日产av | 国产一区二区三区在线免费 | 成人福利视频网 | 成人激情自拍 | 久久93 | 国产免费www |