1.最长公共子序列
1.1题目描述
给定两个字符串 text1 和 text2,返回这两个字符串的最长 公共子序列 的长度。如果不存在 公共子序列 ,返回 0 。
一个字符串的 子序列 是指这样一个新的字符串:它是由原字符串在不改变字符的相对顺序的情况下删除某些字符(也可以不删除任何字符)后组成的新字符串。
例如,"ace" 是 "abcde" 的子序列,但 "aec" 不是 "abcde" 的子序列。
两个字符串的 公共子序列 是这两个字符串所共同拥有的子序列。
示例 1:
输入:text1 = "abcde", text2 = "ace"
输出:3
解释:最长公共子序列是 "ace" ,它的长度为 3 。
示例 2:
输入:text1 = "abc", text2 = "abc"
输出:3
解释:最长公共子序列是 "abc" ,它的长度为 3 。
示例 3:
输入:text1 = "abc", text2 = "def"
输出:0
解释:两个字符串没有公共子序列,返回 0 。
提示:
1 <= text1.length, text2.length <= 1000
text1 和 text2 仅由小写英文字符组成。
1.2题目分析
动态规划
状态转移方程:
$$
dp[i][j] = dp[i-1][j-1] + 1 \qquad\qquad \qquad //text1[i]==text2[j]时
\\
dp[i][j] = max(dp[i][j-1],dp[i-1][j]) \qquad //text1[i]!=text2[j]时
$$
class Solution {
public:
int longestCommonSubsequence(string text1, string text2) {
int n1 = text1.size(), n2 = text2.size();
vector<vector<int>> dp(n1+1, vector<int>(n2+1, 0));//已经将第一行与第一列置为0
for(int i = 1; i <= n1; ++i) {
for(int j = 1; j <= n2; ++j) {
if(text1[i - 1] == text2[j - 1]) {
//两个string的index本身应该从0开始
dp[i][j] = dp[i-1][j-1] + 1;
}else{
dp[i][j] = max(dp[i][j-1], dp[i-1][j]);
}
}//for j
}//for i
return dp[n1][n2];
}
};
/*时间复杂度O(mn),空间复杂度为O(mn)*/
拓展--并输出最长公共子序列(回溯法)
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Solution {
private:
enum type { SIDE, UP, LEFT };
string s1, s2;
vector<vector<int>> dp, record;
void printLCS(int row, int col) const {
if (row == 0 || col == 0) {
return;
}
if (record[row][col] == SIDE) {
printLCS(row - 1, col - 1);
cout << s1[row - 1];
}
else if (record[row][col] == UP) {
printLCS(row, col - 1);
}
else {
printLCS(row - 1, col);
}
}
public:
Solution(string s1, string s2) : s1(s1), s2(s2), dp(s1.length() + 1,
vector<int>(s2.length() + 1, 0)), record(s1.length() + 1, vector<int>(s2.length() + 1, 0)) {
//vector<vector<int>> dp(s1.length() + 1, vector<int>(s2.length() + 1, 0));
//vector<vector<int>> record(s1.length() + 1, vector<int>(s2.length() + 1, 0));
}
int longestCommonSubsequence() {
int n1 = s1.size(), n2 = s2.size();
for (int i = 1; i <= n1; ++i) {
for (int j = 1; j <= n2; ++j) {
if (s1[i - 1] == s2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
record[i][j] = SIDE;
}
else if (dp[i - 1][j] >= dp[i][j - 1]) {
dp[i][j] = dp[i - 1][j];
record[i][j] = LEFT;
}
else {
dp[i][j] = dp[i][j - 1];
record[i][j] = UP;
}
}
}
cout << "最长公共子序列为:";
printLCS(n1, n2);
return dp[n1][n2];
}
};
int main()
{
string s1("abcbdab"), s2("bdcaba");
Solution solution(s1, s2);
int length = solution.longestCommonSubsequence();
cout << endl << "长度为: " << length << endl;
return 0;
}
优化。可以在线性时间内确定dp[i][j]的值是由dp[i-1][j-1]、dp[i][j-1]、dp[i-1][j]中的哪个值确定的,所以可以不需要数组record,也可以在O(m+n)的时间内构造出最长公共子序列。当当前位置的上方和当前位置的最长公共子序列长度相等时,我们就默认选择向上走,而不相等的时候就可以认为当前元素为两个序列的一个公共元素,所以直接添加即可。
void printLCS(int row, int col) const {
if (row == 0 || col == 0) {
return;
}
if (dp[row][col] != dp[row - 1][col]) {
printLCS(row - 1, col - 1);
cout << s1[row - 1];
}
else {
printLCS(row - 1, col);
}
}
递归(lambda递归)
#include <iostream>
#include <string>
#include <vector>
#include <functional>
using namespace std;
const auto& longestCommonSubsequence = [](string& text1, string& text2) {
std::function<int(int, int, string&, string&)> dfs;
dfs = [&](int i, int j, string& text1, string& text2) {
if (i == 0 || j == 0) {
return 0;
}
if (text1[i - 1] == text2[j - 1]) {
return dfs(i - 1, j - 1, text1, text2) + 1;
}
return max(dfs(i - 1, j, text1, text2), dfs(i, j - 1, text1, text2));
};
return dfs(text1.length(), text2.length(), text1, text2);
};
int main()
{
string s1("abcbdab"), s2("bdcaba");
cout << "长度为: " << longestCommonSubsequence(s1, s2) << endl;
return 0;
}
2.最长连续递增序列
2.1题目描述
给定一个未经排序的整数数组,找到最长且 连续递增的子序列,并返回该序列的长度。
连续递增的子序列 可以由两个下标 l 和 r(l < r)确定,如果对于每个 l <= i < r,都有 nums[i] < nums[i + 1] ,那么子序列 [nums[l], nums[l + 1], ..., nums[r - 1], nums[r]] 就是连续递增子序列。
示例 1:
输入:nums = [1,3,5,4,7]
输出:3
解释:最长连续递增序列是 [1,3,5], 长度为3。
尽管 [1,3,5,7] 也是升序的子序列, 但它不是连续的,因为 5 和 7 在原数组里被 4 隔开。
示例 2:
输入:nums = [2,2,2,2,2]
输出:1
解释:最长连续递增序列是 [2], 长度为1。
2.2题目分析
动态规划
class Solution {
public:
int findLengthOfLCIS(vector<int>& nums) {
int n = nums.size();
if(n <= 1) return n;
vector<int> dp(n+1, 1);
int ans = 0;
for(int i = 1; i < n; ++i) {
if(nums[i] > nums[i-1]) {
/*递增*/
dp[i] = dp[i-1] + 1;
}
ans = max(ans, dp[i]);
}
return ans;
}
};
贪心算法
class Solution {
public:
int findLengthOfLCIS(vector<int>& nums) {
int n = nums.size();
if(n <= 1) return n;
int ans = 0, start = 0; //start表示递增子序列的开始位置
for(int i = 1; i < n; ++i) {
if(nums[i] <= nums[i-1]){
start = i;
}
ans = max(ans, i - start + 1);
}
return ans;
}
};
3.最长递增子序列
3.1题目描述
给你一个整数数组 nums ,找到其中最长严格递增子序列的长度。
子序列 是由数组派生而来的序列,删除(或不删除)数组中的元素而不改变其余元素的顺序。例如,[3,6,2,7] 是数组 [0,3,1,6,2,2,7] 的子序列。
示例 1:
输入:nums = [10,9,2,5,3,7,101,18]
输出:4
解释:最长递增子序列是 [2,3,7,101],因此长度为 4 。
示例 2:
输入:nums = [0,1,0,3,2,3]
输出:4
示例 3:
输入:nums = [7,7,7,7,7,7,7]
输出:1
3.2题目分析
借用LCS的代码。将字符串s1增序排列之后形成s2,然后求s1和s2的最大公共子序列。此处略,因为题目给的是vector,不是string,还得修改上述的LCS代码。
动态规划
class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
int n = nums.size();
if(n <= 1) return n;
vector<int> dp(n+1, 1);
for(int i = 0; i < n; ++i) {
for(int j = i; j < n; ++j) {
if(nums[j] > nums[i]) {
dp[j] = max(dp[j], dp[i] + 1);
}
}
}//for i
return *max_element(dp.begin(), dp.end()); //*迭代器 --> 元素值
}
};
/*时间复杂度为O(n^2),空间复杂度为O(n)*/
贪心+二分查找
考虑一个简单的贪心,如果我们要使上升子序列尽可能的长,则我们需要让序列上升得尽可能慢,因此我们希望每次在上升子序列最后加上的那个数尽可能的小。
class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
int len = 1, n = (int)nums.size();
if (n == 0) {
return 0;
}
vector<int> d(n + 1, 0);
d[len] = nums[0];
for (int i = 1; i < n; ++i) {
if (nums[i] > d[len]) {
d[++len] = nums[i];
} else {
int l = 1, r = len, pos = 0; // 如果找不到说明所有的数都比 nums[i] 大,此时要更新 d[1],所以这里将 pos 设为 0
while (l <= r) {
int mid = (l + r) >> 1;
if (d[mid] < nums[i]) {
pos = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
d[pos + 1] = nums[i];
}
}
return len;
}
};
4.最长重复子数组
4.1题目描述
给两个整数数组 nums1 和 nums2 ,返回 两个数组中 公共的 、长度最长的子数组的长度 。
示例 1:
输入:nums1 = [1,2,3,2,1], nums2 = [3,2,1,4,7]
输出:3
解释:长度最长的公共子数组是 [3,2,1] 。
示例 2:
输入:nums1 = [0,0,0,0,0], nums2 = [0,0,0,0,0]
输出:5
4.2题目分析
动态规划
class Solution {
public:
int findLength(vector<int>& nums1, vector<int>& nums2) {
int n1 = nums1.size(), n2 = nums2.size(), ans = 0;
vector<vector<int>> dp(n1+1, vector<int>(n2+1, 0));
for(int i = 1; i <= n1; ++i) {
for(int j = 1; j <= n2; ++j) {
if(nums1[i - 1] == nums2[j - 1]) {
dp[i][j] = dp[i-1][j-1] + 1;
}else{
dp[i][j] = 0;
}
ans = max(ans, dp[i][j]);
}
}a
return ans;
}
};
暴力求解
class Solution {
public:
int findLength(vector<int>& nums1, vector<int>& nums2) {
int n1 = nums1.size(), n2 = nums2.size();
int maxLength = 0, currentLength, m, n;
for(int i = 0; i < n1; ++i) {
for(int j = 0; j < n2; ++j) {
m = i, n = j, currentLength = 0;
while(m < n1 && n < n2 && nums1[m] == nums2[n]) {
++m; ++n;
currentLength++;
}
maxLength = max(maxLength, currentLength);
}
}
return maxLength;
}
};
/*时间复杂度为O(n^3)*/