1. 概述
题目描述:给定两个字符串(或数字序列)A和B,求一个字符串,使得这个字符串是A和B的最长公共部分(子序列可以是补连续的)
例如:“sadstory” 和 “adminstory” 的最长公共子序列是 “adsory”,长度为6
2. 分析
动态规划(Dynamic Programming)
令dp[i][j]表示A的前i个字符和B的前j个字符之间的LCS的长度(下标从1开始),如dp[4][5]表示"sads"和"admin"的LCS长度。那么可以根据A[i]和B[j]的情况,分成两种策略:
- 若A[i] == B[j],则字符串A和B的LCS增加了1位,即有dp[i][j] = dp[i - 1][j - 1] + 1;
- 若A[i] != B[j],则字符串A和字符串B之间的LCS的最大长度继承自dp[i - 1][j]和dp[i][j - 1]的最大者,即dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
边界条件
这里下标计数要从1开始,因为我们要构造边界条件d[i][0] = 0和d[0][j] = 0,表示A的前 i 个字符和B的前0个字符LCS长度为0,A的前0个字符和B的前 j 个字符LCS长度为0.
3. 代码及测试
根据以上分析得到了动态规划的转移方程,就可以写出代码了:
Java
public class Solution {
public static void main(String[] args) {
String A = "ABCBDAB", B = "BDCABA";
int lenA = A.length(), lenB = B.length();
int[][] dp = new int[lenA + 1][lenB + 1];
for (int i = 1; i < lenA + 1; i++) {
for (int j = 1; j < lenB + 1; j++) {
if (A.charAt(i - 1) == B.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
for (int i = 0; i < lenA + 1; i++) {
for (int j = 0; j < lenB + 1; j++) {
System.out.print(dp[i][j] + " ");
}
System.out.println();
}
System.out.println("LCS: " + dp[lenA][lenB]);
}
}
打印出dp矩阵可以得到:
0 0 0 0 0 0 0
0 0 0 0 1 1 1
0 1 1 1 1 2 2
0 1 1 2 2 2 2
0 1 1 2 2 3 3
0 1 2 2 2 3 3
0 1 2 2 3 3 4
0 1 2 2 3 4 4
LCS: 4
C++
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main() {
string A = "ABCBDAB", B = "BDCABA";
int lenA = A.size(), lenB = B.size();
vector<vector<int>> dp(lenA + 1, vector<int>(lenB + 1, 0));
for (int i = 1; i < lenA + 1; i++) {
for (int j = 1; j < lenB + 1; j++) {
if (A[i - 1] == B[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
for (int i = 0; i < lenA + 1; i++) {
for (int j = 0; j < lenB + 1; j++) {
cout << dp[i][j] << " ";
}
cout << endl;
}
cout << "LCS: " << dp[lenA][lenB] << endl;
return 0;
}
0 0 0 0 1 1 1
0 1 1 1 1 2 2
0 1 1 2 2 2 2
0 1 1 2 2 3 3
0 1 2 2 2 3 3
0 1 2 2 3 3 4
0 1 2 2 3 4 4
LCS: 4
4. 求所有的Longest Common Substring
如果要得到具体的最长公共子序列,就需要在得到的dp数组回溯
每次 dp[i][j] = dp[i - 1][j - 1] +1
时LCS就添加一个字符到末尾,因此可以根据这个性质来得到最长子序列。
Java
import java.util.ArrayList;
import java.util.List;
public class Solution {
public static void main(String[] args) {
String A = "ABCBDAB", B = "BDCABA";
int lenA = A.length(), lenB = B.length();
int[][] dp = new int[lenA + 1][lenB + 1];
for (int i = 1; i < lenA + 1; i++) {
for (int j = 1; j < lenB + 1; j++) {
if (A.charAt(i - 1) == B.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
for (int i = 0; i < lenA + 1; i++) {
for (int j = 0; j < lenB + 1; j++) {
System.out.print(dp[i][j] + " ");
}
System.out.println();
}
List<String> res = new ArrayList<>();
allLCS(A, B, dp, lenA, lenB, res, new StringBuilder()); //从dp数组的最后位置向前回溯
System.out.println("LCS: " + dp[lenA][lenB]);
System.out.println(res);
}
public static void allLCS(String A, String B, int[][] dp, int i, int j, List<String> res, StringBuilder str) {
if (i == 0 || j == 0) { // 回溯边界
StringBuilder temp = new StringBuilder(str);
res.add(temp.reverse().toString());
} else {
if (A.charAt(i - 1) == B.charAt(j - 1)) {
str.append(A.charAt(i - 1));
allLCS(A, B, dp, i - 1, j - 1, res, str);
str.deleteCharAt(str.length() - 1); // DFS退出后将当前字符从后面删除
} else {
if (dp[i - 1][j] > dp[i][j - 1]) { // dp[i][j]来自左边,则向左边回溯
allLCS(A, B, dp, i - 1, j, res, str);
} else if (dp[i][j - 1] > dp[i - 1][j]) { // dp[i][j]来自上面,则向上回溯
allLCS(A, B, dp, i, j - 1, res, str);
} else { //两个相等,都可以,因此两个方向都要回溯
allLCS(A, B, dp, i -1, j, res, str);
allLCS(A, B, dp, i, j - 1, res, str);
}
}
}
}
}
0 0 0 0 0 0 0
0 0 0 0 1 1 1
0 1 1 1 1 2 2
0 1 1 2 2 2 2
0 1 1 2 2 3 3
0 1 2 2 2 3 3
0 1 2 2 3 3 4
0 1 2 2 3 4 4
LCS: 4
[BCBA, BCAB, BDAB]
C++
#include <iostream>
#include <vector>
#include <string>
using namespace std;
void allLCS(string A, string B, vector<vector<int>>& dp, int i, int j, vector<string>& res, string str) {
if (i == 0 || j == 0) {
reverse(str.begin(), str.end());
res.push_back(str);
} else {
if (A[i - 1] == B[j - 1]) {
str.push_back(A[i - 1]);
allLCS(A, B, dp, i - 1, j - 1, res, str);
str.pop_back();
} else {
if (dp[i - 1][j] > dp[i][j - 1]) {
allLCS(A, B, dp, i - 1, j, res, str);
}
else if (dp[i][j - 1] > dp[i - 1][j]) {
allLCS(A, B, dp, i, j - 1, res, str);
}
else {
allLCS(A, B, dp, i - 1, j, res, str);
allLCS(A, B, dp, i, j - 1, res, str);
}
}
}
}
int main() {
string A = "ABCBDAB", B = "BDCABA";
int lenA = A.size(), lenB = B.size();
vector<vector<int>> dp(lenA + 1, vector<int>(lenB + 1, 0));
for (int i = 1; i < lenA + 1; i++) {
for (int j = 1; j < lenB + 1; j++) {
if (A[i - 1] == B[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
for (int i = 0; i < lenA + 1; i++) {
for (int j = 0; j < lenB + 1; j++) {
cout << dp[i][j] << " ";
}
cout << endl;
}
cout << "LCS: " << dp[lenA][lenB] << endl;
vector<string> res;
allLCS(A, B, dp, lenA, lenB, res, "");
for (int i = 0; i < res.size(); i++) {
cout << res[i] << " ";
}
cout << endl;
return 0;
}
0 0 0 0 0 0 0
0 0 0 0 1 1 1
0 1 1 1 1 2 2
0 1 1 2 2 2 2
0 1 1 2 2 3 3
0 1 2 2 2 3 3
0 1 2 2 3 3 4
0 1 2 2 3 4 4
LCS: 4
BCBA BCAB BDAB
5. 参考文献
- 算法笔记,胡凡,曾磊等.