zoukankan      html  css  js  c++  java
  • 第五十六题(最长公共子串)

    最长公共字串。
    题目:假设字符串一的全部字符按其在字符串中的顺序出如今另外一个字符串二中,
    则字符串一称之为字符串二的子串。
    注意,并不要求子串(字符串一)的字符必须连续出如今字符串二中。
    请编写一个函数。输入两个字符串。求它们的最长公共子串,并打印出最长公共子串。

    比如:输入两个字符串BDCABA 和ABCBDAB,字符串BCBA 和BDAB 都是是它们的最长公共子串,则输出它们的长度4,并打印随意一个子串。


    能够使用递归或者动态规划求解。递归会反复求解重叠子问题,大量的反复操作导致算法效率非常低,对于长度分别为m,n的字符串,二叉递归树的高度为m+n,因此採用递归方法的复杂度达到O(2^(m+n))。

    动态规划方法採用二维数组来记录前一步的计算结果。避免了反复计算,通过两个for循环就能够解决这个问题,算法复杂度O(mn)。

    最后通过回溯操作获取详细的子串。

    详细的算法原理网上有非常多经典解说,这里不再赘述。

    C++实现代码:

    #include "stdafx.h"
    #include <iostream>
    /*动态规划解决最长公共子序列的问题*/
    
    namespace LongestCommonSubSequence
    {
    	int LCS_Recursive(const char* s1, const char* s2, int index1, int index2)	//採用递归方法
    	{
    		if (s1[index1] == '' || s2[index2] == '')
    			return 0;
    		if (s1[index1] == s2[index2])
    			return 1 + LCS_Recursive(s1, s2, index1 + 1, index2 + 1);
    		else
    		{
    			int len1 = LCS_Recursive(s1, s2, index1 + 1, index2);
    			int len2 = LCS_Recursive(s1, s2, index1, index2+1);
    			return len1 > len2 ?

    len1 : len2; } } //採用动态规划的方法 enum direction { LEFT, UP, LEFTUP }; void printLCS(const char* s1,direction** DPDir, int i, int j) { if (i == 0 || j == 0) return; if (DPDir[i][j] == LEFTUP) { printLCS(s1,DPDir, i - 1, j - 1); cout << s1[i - 1] << "坐标:" << i - 1 << ' ' << j - 1 << endl; } else if (DPDir[i][j] == UP) printLCS(s1, DPDir, i-1, j); else printLCS(s1, DPDir, i, j-1); } int LCS_DP(const char* s1, const const char* s2) { int len1 = strlen(s1); int len2 = strlen(s2); int **DP = (int**)malloc((len1+1)*sizeof(int*)); for (int i = 0; i <= len1; i++) DP[i] = (int*)malloc((len2+1)*sizeof(int)); //memset(DP, 0, sizeof(DP)); 内存错误。DP不是数组名。这里相当于把指向第一行的指针改动了 for (int i = 0; i <= len1; i++) DP[i][0] = 0; for (int j = 0; j < len2; j++) DP[0][j] = 0; DP[0][0] = 1; direction** DPDir = (direction**)malloc((len1 + 1)*sizeof(direction*)); for (int i = 0; i <=len1; i++) DPDir[i] = (direction*)malloc((len2+1)*sizeof(direction)); int temp[8][7]; for (int i = 1; i <= len1;i++) for (int j = 1; j <= len2; j++) { if (s1[i - 1] == s2[j - 1]) { DP[i][j] = DP[i - 1][j - 1] + 1; temp[i][j]=DPDir[i][j] = LEFTUP; } else if (DP[i - 1][j] >=DP[i][j - 1]) { DP[i][j] = DP[i - 1][j]; temp[i][j] = DPDir[i][j] = UP; } else { DP[i][j] = DP[i][j - 1]; temp[i][j] = DPDir[i][j] = LEFT; } } printLCS(s1, DPDir, len1, len2); return DP[len1][len2]; } void test() { const int MAX = 100; char* s1 = "ABCBDAB"; char* s2 = "BDCABA"; char record[MAX]; int length = LCS_Recursive(s1, s2, 0, 0); cout << length << endl; length=LCS_DP(s1, s2); cout << length << endl; } } int _tmain(int argc, _TCHAR* argv[]) { LongestCommonSubSequence::test(); return 0; }


    程序执行结果:



  • 相关阅读:
    id选择器
    HTML列表标签
    HTML表格标签
    HTML常见标签
    javascript代码 调试方法
    圣杯布局和双飞翼布局
    javascript 类型转换。
    javascript的defer和async的区别。
    乱码引起的CSS失效原理,解决技巧。
    浏览器渲染引擎,提高css渲染速度。
  • 原文地址:https://www.cnblogs.com/lxjshuju/p/6729625.html
Copyright © 2011-2022 走看看