如何将字符串保存在最长的常见子序列递归算法中
how to save string in longest common subsequence recursive algorithm
我正在尝试实现最长常见子序列算法的幼稚方法。我正在使用递归方法,将两个字符串传递到功能lcs
中。我成功地计算了最长的子序列中的字符数量。
我的问题是打印LCS的字符。我以为我可以通过将匹配的字符存储在名为sub
的字符串中并将其作为参数传递来做到这一点。但是,我坚持如何保存字符串。我一直在递归方面挣扎,并希望以正确的方式解决此问题的任何技巧。
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
int lcs(string a, string b,string sub){
int aLen = a.length();
int bLen = b.length();
if (aLen==0 || bLen==0){
return 0;
}
if(a.at(aLen-1)==b.at(bLen-1)){
return 1+lcs(a.substr(0,aLen-1),b.substr(0,bLen-1),a.at(aLen-1)+sub); // add letter to subsequence
}
else {
return max(lcs(a.substr(0,aLen-1),b.substr(0,bLen),sub),lcs(a.substr(0,aLen),b.substr(0,bLen-1),sub));
}
}
int main(int argc, const char * argv[])
{
char sub[]="";
int charsInLCS = lcs("sdmc","msdc",sub); //i want to output "sdc"
cout << charsInLCS << endl;
return 0;
}
请小心,您的基本情况是错误的,因为您永远不会对B [0]检查A [0]。此外,通过弦副本非常昂贵,只能通过索引并与之合作的速度更快。我们需要跟踪a[idxa] == b[idxb]
时匹配的字符。这是使用向量的解决方案:
#include <iostream>
#include <string>
#include <queue>
#include <math.h>
#include <algorithm> // std::reverse
using namespace std;
string s1, s2;
int lcs(int idx1, int idx2, vector<char> &v){
if (idx1 == -1 || idx2 == -1){
return 0;
}
if (s1[idx1] == s2[idx2]) {
v.push_back(s1[idx1]); // record that we used this char
return 1 + lcs(idx1 - 1, idx2 - 1, v);
} else {
vector<char> v1, v2;
int p1 = lcs(idx1 - 1, idx2, v1);
int p2 = lcs(idx1, idx2 - 1, v2);
if (p1 > p2) { // we used the chars we already had in v + the ones in v1
v.insert(v.end(), v1.begin(), v1.end());
return p1;
} else { // we used the chars we already had in v + the ones in v2
v.insert(v.end(), v2.begin(), v2.end());
return p2;
}
}
}
int main(int argc, const char * argv[])
{
s1 = "sdmc";
s2 = "msdc";
vector<char> v; // chars we used
int sol = lcs(s1.length() - 1, s2.length() - 1, v); //i want to output "sdc"
cout << sol << " ";
reverse(v.begin(), v.end());
for (auto num : v) {
cout << num;
}
return 0;
}
相关文章:
- 需要为 C++ 中的以下问题设计递归算法
- 这种用于查找连续子数组中最大和的递归算法有什么优势吗?
- 运行合并排序递归算法时EXC_BAD_ACCESS错误
- 如何改进搜索二项式系数的递归算法
- 如何转换多次调用自己的递归算法?
- 如何将字符串保存在最长的常见子序列递归算法中
- 编写递归算法以从链表中删除元素.编写递归算法以将元素添加到链表中
- 指针似乎迷失在递归算法中
- 如何从递归算法返回节点
- 从递归算法到迭代算法
- 可传递值影响递归算法的渐近时间复杂性
- 我的递归算法中的问题,用于查找所有最短、唯一的路径
- 带有两个递归调用的递归算法的时间复杂性
- 这种递归算法有什么问题?
- C++递归算法中抛出异常
- 将条件递归算法转换为迭代算法
- 递归算法中的分段错误
- printbinary递归算法
- 迭代等价于递归算法
- O(n^m) 复杂度的递归算法