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

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

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 7
The longest common sub-sequence is: "include"

Longest Common Subsequence

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,str1
l2 = Length of the second string,str2
f(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

最多兩種情況

  1. LCS of the first string leaving character

    and second string

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

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

    and second string leaving character

    第一個字符串的LCS

    和第二個字符串離開字符

Now, we need to recur down to 0. So,

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

Longest Common Subsequence

Where base cases are,

在基本情況下,

Longest Common Subsequence

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 0
2)  Convert the base case of recursion:
for i=0 to l1
dp[i][0]=0;
for i=0 to l2
dp[0][i]=0;
3)  Fill the DP table as per recursion.
for i=1 to l1    //i be the subproblem length for str1
for j=1 to l2 //j be the subproblem length for str2
if(str1[i-1]==str2[j-1]) //xl1==yl2
dp[i][j]=dp[i-1][j-1]+1;
else
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
end for
end 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;
else
dp[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

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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/377334.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377334.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377334.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

洛必達法則使用條件

使用條件 1、分子分母同趨向于0或無窮大 。 2、分子分母在限定的區域內是否分別可導。 3、當兩個條件都滿足時&#xff0c;再求導并判斷求導之后的極限是否存在&#xff1a;若存在&#xff0c;直接得到答案&#xff1b;若不存在&#xff0c;則說明此種未定式無法用洛必達法則解…

求根號m(巴比倫算法)

巴比倫算法是針對求根號m的近似值情況的&#xff0c;它的思想是這樣的&#xff1a; 設根號mX0,則如果枚舉有答案X(X<X0)&#xff0c;則m/X>X0,當精度要求不高的時候&#xff0c;我們可以看成Xm/XX0,而如果精度要求比較高&#xff0c;我們只需取X和m/X的平均值作為新的枚舉…

Android面試題

http://blog.csdn.net/aomandeshangxiao/article/category/841452 http://www.cppblog.com/life02/category/18316.html轉載于:https://www.cnblogs.com/DonkeyTomy/articles/2598673.html

r語言 分類變量 虛擬變量_R語言中的變量

r語言 分類變量 虛擬變量R語言| 變數 (R Language | Variables) In the previous tutorial, we have come across the basic information that stands as a pavement for understanding the R language in depth. Now moving future let us educate ourselves about the concep…

算法題復習(快排、鏈表、二分、哈希、雙指針)

目錄1、快速排序復習2、鏈表部分復習203. 移除鏈表元素707. 設計鏈表206. 反轉鏈表142.環形鏈表 II3、二分法復習4、哈希法復習5、雙指針復習**15. 三數之和****18. 四數之和****27. 移除元素****344. 反轉字符串**,簡單&#xff0c;雙指針從兩側往中間靠攏&#xff0c;并隨時s…

Cassandra1.2文檔學習(7)—— 規劃集群部署

數據參考&#xff1a;http://www.datastax.com/documentation/cassandra/1.2/webhelp/index.html#cassandra/architecture/architecturePlanningAbout_c.html 當規劃一個Cassandra集群部署時&#xff0c;關于你初始存儲的數據的數據量你應當有一個好的想法&#xff0c;并且對于…

虛擬機設置NAT

需要開啟虛擬機網絡相關服務&#xff0c; 安裝虛擬網卡&#xff0c; 還有必須安裝 VMware ToolsVMware虛擬機下實現NAT方式上網1. 把你的虛擬網卡VMnet8設置為自動獲得IP、自動獲得DNS服務器&#xff0c;啟用。2. 把你虛擬機中操作系統的“本地連接”也設置為自動獲得IP、自動獲…

窗體震動 C# (不使用Timer控件,控制窗體震動)

private static Point plocation new Point(); public static void StartVibration(Form form)//Form 傳入需要振動的窗體 { plocation form.Location; for (int i 1; i < 41; i)//41&#xff0c;可以理解為震動的時間。…

算法題復習(棧與隊列、二叉樹)

目錄棧與隊列棧用于匹配的問題隊列用于堆二叉樹系列深度遍歷&#xff0c;遞歸與迭代層序遍歷二叉樹屬性二叉樹修改與構造二叉搜索樹公共祖先二叉搜索樹的修改與構造棧與隊列 棧用于匹配的問題 20. 有效的括號 https://leetcode-cn.com/problems/valid-parentheses/ 不匹配的三…

bpsk_BPSK的完整形式是什么?

bpskBPSK&#xff1a;二進制相移鍵控 (BPSK: Binary Phase Shift Keying) BPSK is an abbreviation of "Binary Phase Shift Keying". BPSK是“二進制相移鍵控”的縮寫 。 BPSK is also occasionally called phase reversal keying (PRK), or 2PSK, which is the el…

win7 下安裝oracle 10g

oracle 10g 在win7下安裝&#xff0c;提示程序異常終止&#xff0c;發生未知錯誤 在網上搜結果&#xff1a; 修改Oracle 10G\database\stage\prereq\db\refhost.xml 在 </SYSTEM> <CERTIFIED_SYSTEMS>后面添加 <!--Microsoft Windows 7--> <OPERAT…

poj 1703 Find them, Catch them

題目鏈接&#xff1a;http://poj.org/problem?id1703 題目大意&#xff1a;警察抓獲N個罪犯&#xff0c;這些罪犯只可能屬于兩個團伙中的一個&#xff0c;現在給出M個條件&#xff08;D a b表示a和b不在同一團伙&#xff09;&#xff0c;對于每一個詢問(A a b)確定a&#xff0…

雙向a*搜索算法_雙向搜索算法

雙向a*搜索算法什么是雙音搜索&#xff1f; (What is bitonic search?) Searching a bitonic array is known as bitonic search. An array is said to be bitonic if it has an increasing sequence of integers followed immediately by a decreasing sequence of integers.…

關于LRU緩存簡單記錄以及代碼補全。

目錄大概思路時間空間復雜度分析指針操作具體細節代碼雙向鏈表設計私有成員變量設計:構造函數和析構函數設計&#xff1a;get與put具體設計雙向指針的具體細節添加到頭節點函數刪除尾節點函數刪除節點函數刪除節點函數感想今天面試考到LRU&#xff0c;太緊張了&#xff0c;完全…

碼農干貨系列【4】--圖像識別之矩形區域搜索

簡介 定位某個圖片的矩形區域是非常有用的&#xff0c;這個可以通過手動的選擇某個區域來實現定位&#xff0c;圖片相關的軟件都提供了這個功能&#xff1b;也可以像本篇一個通過程序來實現智能定位。前者會有誤差&#xff0c;效率低下&#xff1b;后者選區精度高&#xff0c;效…

算法題復習(回溯)

目錄base code棋盤問題51. N 皇后37. 解數獨組合問題77. 組合未剪枝優化剪枝優化216. 組合總和 III未剪枝優化剪枝優化17. 電話號碼的字母組合39. 組合總和未剪枝優化剪枝優化40. 組合總和 II,挺重要的&#xff0c;涉及到去重了切割問題131. 分割回文串子集問題78. 子集90. 子集…

pfa是什么意思_PFA的完整形式是什么?

pfa是什么意思PFA&#xff1a;預測性故障分析 (PFA: Predictive Failure Analysis) PFA is an abbreviation of Predictive Failure Analysis. It is a technique of a mechanism of the computer that is used to predict impending failures of software or hardware compone…

SqlServer視圖(view)

--上節回顧--1.什么是事務--2.事務的特征--原子性、一致性、隔離性、持久性--3.與事務相關的t-sql命令--開啟事務&#xff1a;begin transaction--提交事務&#xff1a;commit transaction--回滾事務&#xff1a;rollback transaction ----------------視圖-------------------…

Android中的廣播Broadcast詳解

今天來看一下Android中的廣播機制&#xff0c;我們知道廣播Broadcast是Android中的四大組件之一&#xff0c;可見他的重要性了&#xff0c;當然它的用途也很大的&#xff0c;比如一些系統的廣播&#xff1a;電量低、開機、鎖屏等一些操作都會發送一個廣播&#xff0c;具體的And…

sql check約束_在SQL中使用CHECK約束

sql check約束Basically, CHECK constraint is used to LIMIT in columns for the range of values. We can use this constraint for single or multiple columns. 基本上&#xff0c; CHECK約束用于限制值范圍內的列 。 我們可以將此約束用于單列或多列。 In single column,…