最长公共子串
来源:互联网 发布:apache 下载 编辑:程序博客网 时间:2024/06/14 10:44
定义
子串即为一个字符的一个子集,例如字符串s="abc"
那么字符串s
的子串则包括a,b,c, ab, bc, abc
。注意到它们是连续的。公共子串则是两个或多个字符串中相同的子字符串。例如字符串s1="abcba", s2="bcbda"
,则有a,b,c, bc, bcb
均是s1,s1
的子串,则称它们为字符串s1,s2
的公共子串。最长公共子串则是所有公共子串中长度最长的子字符串。
公共子字符串与公共子序列十分类似,唯一不同的是公共子字符串必须是连续的,而公共子序列则不一定是连续的。
说明
假设有两个字符串分别为s1=”abcdef”,s2=”bcdefa”,要求出两个字符串的最长公共字串,通常可以采用暴力求解的方法来实现,这种方法思路比较简单,但是时间复杂度较高,此处不做过多说明。
更为一般的解决办法是通过空间换时间的方式,来降低运行时间。对于s1,s2,我们可以构建int arr[s1.length()][s2.length()];
的二维数组,如下所示:
| b c d e f a-+------------a| 0 0 0 0 0 1b| 1 0 0 0 0 0c| 0 2 0 0 0 0d| 0 0 3 0 0 0e| 0 0 0 4 0 0f| 0 0 0 0 5 0
通过上面的二维数组,我们可以很容易的发现,字符串s1,s2最长公共字串长度为5,即bcdef。那么要如何计算该二维数组的值呢?为了计算矩阵的值,可以分为两类情况:
- 矩阵的第一行或第一列情况下的处理
- 矩阵其他行列的处理
首先看第二类情况,在这种情况下,可以很容易的得到,当s1[i] = s2[j]
时,有arr[i][j] = arr[i-1][j-1] + 1
。第一种情况由于i-1
或j-1
会产生越界现象,因此需要单独处理,此时,当s1[i] = s2[j]
时,有arr[i][j] = 1
。上述两种情况下,当s1[i] != s2[j]
,有arr[i][j] = 0
。
实现
给定两个字符串,求出两个字符串的最长公共字符串。
#include <iostream>#include <string>#include <vector>typedef std::vector<int> array1d;typedef std::vector<array1d> array2d;static bool show_array2d = false;void print_array2d(const array2d& array){ if (!show_array2d) { return; } size_t rows = array.size(); size_t cols = array[0].size(); for (size_t i = 0; i < rows; ++i) { for (size_t j = 0; j < cols; ++j) { std::cout <<array[i][j] <<" "; } std::cout <<std::endl; }}std::vector<std::string> get_lcsubstr(const std::string& s1, const std::string& s2){ size_t len1 = s1.length(); size_t len2 = s2.length(); size_t max_len = 0; array2d tmp(len1, array1d(len2)); for (size_t i = 0; i < len1; ++i) { for (size_t j = 0; j < len2; ++j) { if (i == 0 || j == 0) { if (s1[i] == s2[j]) { tmp[i][j] = 1; max_len = 1 > max_len ? 1 : max_len; } else { tmp[i][j] = 0; } } else { if (s1[i] == s2[j]) { tmp[i][j] = tmp[i-1][j-1] + 1; max_len = tmp[i][j] > max_len ? tmp[i][j] : max_len; } else { tmp[i][j] = 0; } } } } print_array2d(tmp); std::vector<std::string> lcss; for (int i = len1 - 1; i >= 0; --i) { for (int j = len2 - 1; j >= 0; --j) { if (tmp[i][j] == max_len) { lcss.push_back(s1.substr(i-max_len+1, max_len)); j--; } } } return lcss;}int main(int argc, char **argv){ if (argc != 3 && argc != 4) { std::cout <<"usage: substring [--show-array] s1 s2" <<std::endl; return 1; } if (argc == 4 && std::string("--show-array") != argv[1]) { std::cout <<"invalid option" <<std::endl; return 1; } else if (argc == 4) { show_array2d = true; } std::string s1(argv[argc-1]); std::string s2(argv[argc-2]); std::vector<std::string> lcss = get_lcsubstr(s1, s2); for (int i = 0; i < lcss.size(); ++i) { std::cout <<lcss[i] <<std::endl; } return 0;}
- 最长公共子串
- 最长公共子串
- 最长公共子串
- 最长公共子串
- 最长公共子串
- 最长公共子串
- 最长公共子串
- 最长公共子串
- 最长公共子串
- 最长公共子串
- 最长公共子串
- 最长公共子串
- 最长公共子串
- 最长公共子串
- 最长公共子串
- 最长公共子串
- 最长公共子串
- 最长公共子串
- 109. Convert Sorted List to Binary Search Tree
- 对java中static关键字的理解
- Linux运维必学必会(一)- sed命令
- CodeForces 24D Broken robot (三对角矩阵消元)
- 5-1 厘米换算英尺英寸 (15分)
- 最长公共子串
- Leetcode7. Reverse Integer
- 文本特征提取方法的研究
- Spring面试问答Top 25
- 利用反射修改drawerLayout滑动边距
- jboss eap 6.3 域(Domain)模式配置
- 动态代理proxy
- js禁用鼠标右键菜单(加强版)
- HBase基本数据操作详解