最長公共子序列的問題經常使用於解決字符串的類似度,是一個很是實用的算法,做爲碼農,此算法是咱們的必備基本功。算法
舉個例子,cnblogs這個字符串中子序列有多少個呢?很顯然有27個,好比其中的cb,cgs等等都是其子序列,咱們能夠看出子序列不見得必定是連續的,連續的那是子串。我想你們已經瞭解了子序列的概念,那如今能夠延伸到兩個字符串了,你能夠看出 cnblogs 和 belong 的公共子序列嗎?在你找出的公共子序列中,你能找出最長的公共子序列嗎?數組
從圖中能夠看到最長公共子序列爲blog,仔細想一想咱們能夠發現其實最長公共子序列的個數不是惟一的,可能會有兩個以上,可是長度必定是惟一的,好比這裏的最長公共子序列的長度爲4。數據結構
1. 枚舉法ide
這種方法是最簡單,也是最容易想到的,固然時間複雜度也是龜速的,能夠分析一下,剛纔也說過了cnblogs的子序列個數有27個 ,延伸一下:一個長度爲N的字符串,其子序列有2N個,每一個子序列要在第二個長度爲N的字符串中去匹配,匹配一次須要O(N)的時間,總共也就是O(N*2N),能夠看出,時間複雜度爲指數級,恐怖的使人窒息。函數
2. 動態規劃優化
既然是經典的題目確定是有優化空間的,而且解題方式是有固定流程的,這裏咱們採用的是矩陣實現,也就是二維數組。3d
遞推方程爲:code
不知道你們看懂了沒?動態規劃的一個重要性質特色就是解決「子問題重疊」的場景,能夠有效的避免重複計算,根據上面的公式其實能夠發現C[i,j]一直保存着當前(Xi,Yi)的最大子序列長度,代碼以下:blog
public class Program { static int[,] martix; static string str1 = "cnblogs"; static string str2 = "belong"; static void Main(string[] args) { martix = new int[str1.Length + 1, str2.Length + 1]; LCS(str1, str2); //只要拿出矩陣最後一個位置的數字便可 Console.WriteLine("當前最大公共子序列的長度爲:{0}", martix[str1.Length, str2.Length]); Console.Read(); } static void LCS(string str1, string str2) { //初始化邊界,過濾掉0的狀況 for (int i = 0; i <= str1.Length; i++) martix[i, 0] = 0; for (int j = 0; j <= str2.Length; j++) martix[0, j] = 0; //填充矩陣 for (int i = 1; i <= str1.Length; i++) { for (int j = 1; j <= str2.Length; j++) { //相等的狀況 if (str1[i - 1] == str2[j - 1]) { martix[i, j] = martix[i - 1, j - 1] + 1; } else { //比較「左邊」和「上邊「,根據其max來填充 if (martix[i - 1, j] >= martix[i, j - 1]) martix[i, j] = martix[i - 1, j]; else martix[i, j] = martix[i, j - 1]; } } } } }
圖你們能夠本身畫一畫,代碼徹底是根據上面的公式照搬過來的,長度的問題咱們已經解決了,此次要解決輸出最長子序列的問題,採用一個標記函數 Flag[i,j],當字符串
①:C[i,j]=C[i-1,j-1]+1 時標記Flag[i,j]="left_up"; (左上方箭頭)
②:C[i-1,j]>=C[i,j-1] 時標記Flag[i,j]="left"; (左箭頭)
③: C[i-1,j]<C[i,j-1] 時 標記Flag[i,j]="up"; (上箭頭)
例如:我輸入兩個序列X=acgbfhk,Y=cegefkh。
public class Program { static int[,] martix; static string[,] flag; static string str1 = "acgbfhk"; static string str2 = "cegefkh"; static void Main(string[] args) { martix = new int[str1.Length + 1, str2.Length + 1]; flag = new string[str1.Length + 1, str2.Length + 1]; LCS(str1, str2); //打印子序列 SubSequence(str1.Length, str2.Length); Console.Read(); } static void LCS(string str1, string str2) { //初始化邊界,過濾掉0的狀況 for (int i = 0; i <= str1.Length; i++) martix[i, 0] = 0; for (int j = 0; j <= str2.Length; j++) martix[0, j] = 0; //填充矩陣 for (int i = 1; i <= str1.Length; i++) { for (int j = 1; j <= str2.Length; j++) { //相等的狀況 if (str1[i - 1] == str2[j - 1]) { martix[i, j] = martix[i - 1, j - 1] + 1; flag[i, j] = "left_up"; } else { //比較「左邊」和「上邊「,根據其max來填充 if (martix[i - 1, j] >= martix[i, j - 1]) { martix[i, j] = martix[i - 1, j]; flag[i, j] = "left"; } else { martix[i, j] = martix[i, j - 1]; flag[i, j] = "up"; } } } } } static void SubSequence(int i, int j) { if (i == 0 || j == 0) return; if (flag[i, j] == "left_up") { Console.WriteLine("{0}: 當前座標:({1},{2})", str2[j - 1], i - 1, j - 1); //左前方 SubSequence(i - 1, j - 1); } else { if (flag[i, j] == "up") { SubSequence(i, j - 1); } else { SubSequence(i - 1, j); } } } }
因爲直接繪圖很麻煩,嘿嘿,我就用手機拍了張:
好,咱們再輸入兩個字符串:
static string str1 = "abcbdab"; static string str2 = "bdcaba";
經過上面的兩張圖,咱們來分析下它的時間複雜度和空間複雜度。