最长公共子序列的问题经常使用于解决字符串的类似度,是一个很是实用的算法,做为码农,此算法是咱们的必备基本功。算法
举个例子,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";
经过上面的两张图,咱们来分析下它的时间复杂度和空间复杂度。