#include <iostream>
#include <bits/stdc++.h>
using namespace std;
/*
* 最长公共子串问题-动态规划
* dp[i][j]表示在必须将str1[i-1]和str2[j-1]作为公共子串最后一个字符的情况下,公共子串最大长度
* dp[i][j]取决于dp[i-1][j], dp[i][j-1], dp[i-1][j-1]
* 时间复杂度O(MN) 空间复杂度O(MN)
*/
class Solution {
public:
// 最长公共子串长度
int lengthofLCST(string &str1, string &str2) {
if(str1.empty() || str2.empty()) return 0;
vector<vector<int>> dp(str1.size()+1, vector<int>(str2.size()+1, 0));
int ret = 0;
for (int i = 1; i <= str1.size(); ++i) {
for (int j = 1; j
最长公共子串(动态规划)
最新推荐文章于 2024-02-27 11:20:50 发布