我已经建立了递归关系,它找到了两个字符串之间最长的连续公共字符串,我怎么能跳过其中一个字符串中的一个字符

I have made recurrence relation which finds the longest contiguous common string between two strings how can i skip one char in my one of the string

本文关键字:字符串 一个 连续 字符 怎么能 找到了 建立 关系 递归 之间 两个      更新时间:2023-10-16
int lcs(int i, int j, int count) 
{ 

if (i == 0 || j == 0) 
return count; 

if (X[i-1] == Y[j-1]) { 
count = lcs(i - 1, j - 1, count + 1); 
} 

count = max(count, max(lcs( i, j - 1, 0), lcs( i - 1, j, 0))); 
return count; 
} 

例如: X 包含 AABB,Y 包含 AACB,我希望我的递归关系跳过 c 以给出 AAB 作为 lCCS,这就是我到目前为止所做的。

假设你使用std::string,你可以通过它的erase从字符串中删除一个字符:

std::string A{"AACB"};
std::string copy = A;  // make a copy to keep the original
copy.erase(2);         // erase character at pos 2
std::cout << copy;     // prints AAB

如果您事先不知道要删除哪个字符,则可以尝试循环中的所有字符,并在循环中调用 forX的函数,并在删除一个字符后调用另一个字符串Y

相关文章: