#include<iostream>
#include<string>
using namespace std;
int getLCSLength(string str1, string str2){
int **temp= new int*[(str1.length())*(str2.length())]; //声明一个二维数组,存储最长公共子串长度
int length = 0; //最长公共子串长度
for (int i = 0; i < str1.length(); i++) {
for (int j = 0; j < str2.length(); j++) {
if (str1[i] ==str2[j]){
if (i > 0 && j > 0){
temp[i][j] = temp[i - 1][j - 1] + 1;
}
else{
temp[i][j] = 1;
}
if (temp[i][j] > length){ //当前元素值大于最大公共子串长度
length = temp[i][j];
}
}
else{
temp[i][j] = 0;
}
}
}
return length;
}
求两个字符串中的最长公共子串的长度
最新推荐文章于 2022-04-10 16:27:29 发布