动态规划算法---求最长公共子序列

最长公共子序列和最长公共子串区别

       最长公共子串(Longest Common Substring)最长公共子序列(Longest Common Subsequence)的区别: 子串要求在原字符串中是连续的,而子序列则只需保持相对顺序一致,并不要求连续。例如X = {a, Q, 1, 1}; Y = {a, 1, 1, d, f}那么,{a, 1, 1}是X和Y的最长公共子序列,但不是它们的最长公共字串。

一、最长公共子序列

具体的算法思想参考以下文章:

http://blog.csdn.net/lisonglisonglisong/article/details/41548557

http://blog.csdn.net/zhongkeli/article/details/8847694


只求最长子序列长度


如果仅仅需要知道最长子序列的长度值,代码如下:

[cpp]  view plain  copy
  1. #include <vector>  
  2. #include <string>  
  3. #include <iostream>  
  4. #include <string.h>  
  5. #include <sstream>  
  6. using namespace std;  
  7.   
  8. //最长公共子串(LCS)  
  9. //二维数组veca记录的是两个字符串Xi和Yj的LCS长度  
  10. int LCS_length(const string &str1, const string &str2, vector<vector<int> > &veca) {  
  11.     int i, j;  
  12.     int biggest = 0;  
  13.     if (str1 == "" || str2 == "")  
  14.         return 0;  
  15.   
  16.     for (i = 0; i <= str1.length(); i++) {    //初始化veca[][]
  17.         veca[i][0] = 0;  
  18.     }  
  19.     for (j = 0; j <= str2.length(); j++) {  ////初始化veca[][]
  20.         veca[0][j] = 0;  
  21.     }  
  22.     for (i = 1; i <= str1.length(); i++) {  
  23.         for (j = 1; j <= str2.length(); j++) {  
  24.             if (str1[i - 1] == str2[j - 1]) {  
  25.                 veca[i][j] = veca[i - 1][j - 1] + 1;  
  26.             }  
  27.             else {  
  28.                 if (veca[i - 1][j] >= veca[i][j - 1])  
  29.                     veca[i][j] = veca[i - 1][j];  
  30.                 else  
  31.                     veca[i][j] = veca[i][j-1];  
  32.             }  
  33.         }  
  34.     }  
  35.     return veca[str1.length()][str2.length()];  
  36. }  
  37.   
  38. int main() {  
  39.     string input;  
  40.     getline(cin, input);  
  41.         stringstream ss(input);  
  42.         string str1, str2;  
  43.     ss >> str1;  
  44.     ss >> str2;  
  45.     //将veca初始化为一个二维数组,其行列值分别为str1和str2的长度加1  
  46.     //二维数组veca记录的是两个字符串Xi和Yj的LCS长度  
  47.     vector<vector<int> > veca(str1.length() + 1, vector<int>(str2.length() + 1));  
  48.     cout << LCS_length(str1, str2, veca) << endl;  
  49.     return 0;  
  50. }  
结果:


动态规划解决LCS问题的时间复杂度为 O(mn),这比简单的递归实现要快多了。空间复杂度是O(mn),因为使用了一个动态规划表。

要输出一个LCS的内容

和上面的程序比,只需要多一个二维数组记录在遍历中所选择的子问题的最优解即可。如下程序:

[cpp]  view plain  copy
  1. //输出最长公共子串(LCS)  
  2. //二维数组veca记录的是两个字符串Xi和Yj的LCS长度  
  3. int LCS_length(const string &str1, const string &str2,   
  4.                 vector<vector<int> > &veca, vector<vector<int> > &vecb) {  
  5.     int i, j;  
  6.     int biggest = 0;  
  7.     if (str1 == "" || str2 == "")  
  8.         return 0;  
  9.   
  10.     for (i = 0; i <= str1.length(); i++) {  
  11.         veca[i][0] = 0;  
  12.     }  
  13.     for (j = 0; j <= str2.length(); j++) {  
  14.         veca[0][j] = 0;  
  15.     }  
  16.     for (i = 1; i <= str1.length(); i++) {  
  17.         for (j = 1; j <= str2.length(); j++) {  
  18.             //如果Xi-1 == Yj-1,那么最长子序列为veca[i - 1][j - 1] + 1  
  19.             //此时将vecb[i][j] = 1表明str1[i-1]是子问题LCS的一个元素  
  20.             if (str1[i - 1] == str2[j - 1]) {  
  21.                 veca[i][j] = veca[i - 1][j - 1] + 1;  
  22.                 vecb[i][j] = 1;  
  23.             }  
  24.             else {  
  25.                 if (veca[i - 1][j] >= veca[i][j - 1]) {  
  26.                     veca[i][j] = veca[i - 1][j];  
  27.                     vecb[i][j] = 2;  
  28.                 }  
  29.                 else {  
  30.                     veca[i][j] = veca[i][j-1];  
  31.                     vecb[i][j] = 3;  
  32.                 }  
  33.             }  
  34.         }  
  35.     }  
  36.     return veca[str1.length()][str2.length()];  
  37. }  
  38.   
  39. //该函数用于输出一个LCS的序列  
  40. //这里输出的顺序是先向上寻找,再向左寻找  
  41. void PrintOneLCS(vector<vector<int> > &vecb, string &str1, int i, int j) {  
  42.     if (i == 0 || j == 0)  
  43.         return;  
  44.     if (vecb[i][j] == 1) {  
  45.         PrintOneLCS(vecb, str1, i - 1, j - 1);  
  46.         cout << str1[i - 1] << " ";  
  47.     }  
  48.     else if (vecb[i][j] == 2)  
  49.         PrintOneLCS(vecb, str1, i -1, j);  
  50.     else  
  51.         PrintOneLCS(vecb, str1, i, j - 1);  
  52. }  
  53.   
  54. int main() {  
  55.     string input;  
  56.     getline(cin, input);  
  57.     stringstream ss(input);  
  58.     string str1, str2;  
  59.     ss >> str1;  
  60.     ss >> str2;  
  61.     //将veca初始化为一个二维数组,其行列值分别为str1和str2的长度加1  
  62.     //二维数组veca记录的是两个字符串Xi和Yj的LCS长度  
  63.     //二维数组vecb[i][j]记录veca[i][j]时所选择的子问题的最优解  
  64.     vector<vector<int> > veca(str1.length() + 1, vector<int>(str2.length() + 1));  
  65.     vector<vector<int> > vecb(str1.length() + 1, vector<int>(str2.length() + 1));  
  66.     cout << LCS_length(str1, str2, veca, vecb) << endl;  
  67.     PrintOneLCS(vecb, str1, str1.length(), str2.length());  
  68.     return 0;  
  69. }  

求一个LCS内容也可以不借助辅助二维数组vecb而是用下面小节的方法,

[cpp]  view plain  copy
  1. //该函数用于输出一个LCS的序列,使用下一小节的方法  
  2. //这里输出的顺序是先向左寻找,再向上寻找  
  3. void PrintOneLCS(string &str1, string &str2, int i, int j,   
  4.     vector<vector<int> > &veca) {  
  5.         string lcs_str;  
  6.         while (i > 0 && j > 0) {  
  7.             if (str1[i - 1] == str2[j - 1]) {  
  8.                 lcs_str = str1[i - 1] + lcs_str;  
  9.                 --i;  
  10.                 --j;  
  11.             }  
  12.             else {  
  13.                 //如果左边存在LCS就从左边找否则再从右边找  
  14.                 if (veca[i - 1][j] >= veca[i][j - 1])  
  15.                     --i;  
  16.                 else  
  17.                     --j;  
  18.             }  
  19.         }  
  20.         cout << lcs_str << endl;  
  21. }  

如下代码:


要输出所有LCS的内容

两个字符串对应的最长公共子序列不一定唯一,这个程序输出所有的LCS内容。

基本思想是:


具体参考文章:http://blog.csdn.net/lisonglisonglisong/article/details/41596309

代码:

[cpp]  view plain  copy
  1. #include <vector>  
  2. #include <iomanip>  
  3. #include <set>  
  4. #include <string>  
  5. #include <map>  
  6. #include <iostream>  
  7. #include <string.h>  
  8. #include <sstream>  
  9. using namespace std;  
  10.   
  11. set<string> all_lcs; //注意这里要用set去除重复的LCS  
  12. //最长公共子串(LCS)  
  13. //二维数组veca[i][j]记录的是两个字符串Xi和Yj的LCS长度  
  14. int LCS_length(const string &str1, const string &str2, vector<vector<int> > &veca) {  
  15.     int i, j;  
  16.     int biggest = 0;  
  17.     if (str1 == "" || str2 == "")  
  18.         return 0;  
  19.   
  20.     for (i = 0; i <= str1.length(); i++) {  
  21.         veca[i][0] = 0;  
  22.     }  
  23.     for (j = 0; j <= str2.length(); j++) {  
  24.         veca[0][j] = 0;  
  25.     }  
  26.     for (i = 1; i <= str1.length(); i++) {  
  27.         for (j = 1; j <= str2.length(); j++) {  
  28.             if (str1[i - 1] == str2[j - 1]) {  
  29.                 veca[i][j] = veca[i - 1][j - 1] + 1;  
  30.             }  
  31.             else {  
  32.                 if (veca[i - 1][j] >= veca[i][j - 1])  
  33.                     veca[i][j] = veca[i - 1][j];  
  34.                 else  
  35.                     veca[i][j] = veca[i][j-1];  
  36.             }  
  37.         }  
  38.     }  
  39.     return veca[str1.length()][str2.length()];  
  40. }  
  41.   
  42. //该函数找出所有的LCS的序列,并将其存在vector中  
  43. void PrintAllLCS(string &str1, string &str2, int i, int j,   
  44.                  vector<vector<int> > &veca, string lcs_str) {  
  45. //注意这里形参lcs_str不可以为引用,这里需要每次调用lcs_str都重新生成一个对象  
  46.     while (i > 0 && j > 0) {  
  47.         if (str1[i - 1] == str2[j - 1]) {  
  48.             lcs_str = str1[i - 1] + lcs_str; //逆向存放  
  49.             --i;  
  50.             --j;  
  51.         }  
  52.         else {  
  53.             if (veca[i - 1][j] > veca[i][j - 1]) //向左走  
  54.                 --i;  
  55.             else if (veca[i - 1][j] < veca[i][j - 1]) //向上走  
  56.                 --j;  
  57.             else { //此时向上向右均为LCS的元素  
  58.                 PrintAllLCS(str1, str2, i - 1, j, veca, lcs_str);  
  59.                 PrintAllLCS(str1, str2, i, j - 1, veca, lcs_str);  
  60.                 return;  
  61.             }  
  62.         }  
  63.     }  
  64.     cout << "   " << lcs_str << endl;  
  65.     all_lcs.insert(lcs_str);  
  66. }  
  67. int main() {  
  68.     string input;  
  69.     getline(cin, input);  
  70.     stringstream ss(input);  
  71.     string str1, str2;  
  72.     ss >> str1;  
  73.     ss >> str2;  
  74.     //将veca初始化为一个二维数组,其行列值分别为str1和str2的长度加1  
  75.     //二维数组veca记录的是两个字符串Xi和Yj的LCS长度  
  76.     vector<vector<int> > veca(str1.length() + 1, vector<int>(str2.length() + 1));  
  77.     cout << LCS_length(str1, str2, veca) << endl;  
  78.   
  79.     string lcs_str;  
  80.     PrintAllLCS(str1, str2, str1.length(), str2.length(), veca, lcs_str);  
  81.     set<string>::iterator iter = all_lcs.begin();  
  82.     while (iter != all_lcs.end()) {  
  83.         cout << *iter++ << endl;  
  84.     }  
  85.     return 0;  
  86. }  

如图所示的两个字符串共有三个LCS。

二、最长公共子串

描述:

计算两个字符串的最大公共子串(Longest Common Substring)的长度,字符不区分大小写。

输入:

输入两个字符串

输出:

输出一个整数

样例输入:

asdfas werasdfaswer

样例输出:

6


这里的最大公共字串要求的字串是连续的。

求字串的方法和求子序列方法类似

当str1[i] == str2[j]时,子序列长度veca[i][j] = veca[i - 1][j - 1] + 1;只是当str1[i] != str2[j]时,veca[i][j]长度要为0,而不是max{veca[i - 1][j], veca[i][j - 1]}。

[cpp]  view plain  copy
  1. #include <vector>    
  2. #include <string>    
  3. #include <iostream>    
  4. #include <string.h>    
  5. #include <sstream>    
  6. using namespace std;    
  7.     
  8. int LCS_length(const string &str1, const string &str2, vector<vector<int> > &veca) {    
  9.     int i, j;    
  10.     int biggest = 0;    
  11.     if (str1 == "" || str2 == "")    
  12.         return 0;    
  13.     for (i = 0; i <= str1.length(); i++) {    
  14.         veca[i].resize(str2.length() + 1, 0);    
  15.     }    
  16.     for (j = 0; j <= str2.length(); j++) {    
  17.         veca[0][j] = 0;    
  18.     }    
  19.     for (i = 1; i <= str1.length(); i++) {    
  20.         for (j = 1; j <= str2.length(); j++) {    
  21.             if (str1[i - 1] == str2[j - 1]) {    
  22.                 veca[i][j] = veca[i - 1][j - 1] + 1;    
  23.                 if (veca[i][j] > biggest)    
  24.                     biggest = veca[i][j];    
  25.             }    
  26.             else    
  27.    //可以看出,求最长子串和求最长子序列差别仅仅在这里  
  28.                 veca[i][j] = 0;    
  29.                 
  30.         }    
  31.     }    
  32.     return biggest;    
  33. }    
  34.     
  35. int main() {    
  36.     string input;    
  37.     getline(cin, input);    
  38.     stringstream ss(input);    
  39.     string str1;    
  40.     ss >> str1;    
  41.     string str2;    
  42.     ss >> str2;    
  43.     vector<vector<int> > veca(str1.length() + 1);    
  44.     cout << LCS_length(str1, str2, veca) << endl;    
  45.     return 0;    
  46. }    
同样适用求最长子序列的测试数据,得到它们的公共最长子串长度为2,而它们的公共最长子序列长度为4.


三、动态规划的其它题目

1、硬币面值组合问题

           除了动态规划,该题还有其他解法。

3、数组最大子数组和的最大值


4、计算两个字符串的相似度(编程之美)
       该文章原理说得比较清楚: 点击打开链接
       这里是代码: 点击打开链接

5、求每一对顶点之间的最短路径:Floyd-Warshall算法


转自:https://blog.csdn.net/u013074465/article/details/45392687

猜你喜欢

转载自blog.csdn.net/qq_34793133/article/details/80580248
今日推荐