c++ - 最长公共(public)子序列算法递归解的记忆化

标签 c++ algorithm

当我尝试内存最长公共(public)子序列问题的递归解时,内存的 soln 返回不同的答案。我不太明白为什么......

#include <iostream>
#include <map>
#include <string>
#include <utility>
using namespace std;

string char_to_string(char c) { return string(1, c); }

map< pair<string, string>, string > hash;

// CORRECTED ANSWER AS PER DUKE'S SOLUTION - THANKS!
string lcsRec(string s1, string s2, string lcs = "") {
    pair<string, string> s1s2 = make_pair(s1, s2);
    pair< pair<string, string>, string> lcsTriplet = make_pair(s1s2, lcs);

    if (hash.count(lcsTriplet)) {
        return hash[lcsTriplet];
    }

    if (s1.size() == 0 || s2.size() == 0)
        return hash[lcsTriplet] = lcs;

    string s1Minus1 = s1.substr(0, s1.size() - 1);
    string s2Minus1 = s2.substr(0, s2.size() - 1);

     if (s1[s1.size() - 1] == s2[s2.size() - 1])
        return hash[lcsTriplet] = lcsRec(s1Minus1, s2Minus1, char_to_string(s1[s1.size() - 1]) + lcs);

    string omits1 = lcsRec(s1Minus1, s2, lcs);
    string omits2 = lcsRec(s1, s2Minus1, lcs);

    return hash[lcsTriplet] = (omits1.size() > omits2.size()) ? omits1 : omits2;
}

// MEMOIZED SOLUTION
string lcsRec(string s1, string s2, string lcs = "") {
    pair<string, string> p0 = make_pair(s1, s2);

    if (hash.count(p0)) return hash[p0]; 

    if (s1.size() == 0 || s2.size() == 0)
        return hash[p0] = lcs;

    string s1Minus1 = s1.substr(0, s1.size() - 1);
    string s2Minus1 = s2.substr(0, s2.size() - 1);

     if (s1[s1.size() - 1] == s2[s2.size() - 1])
        return hash[p0] = lcsRec(s1Minus1, s2Minus1, char_to_string(s1[s1.size() - 1]) + lcs);

    string omits1 = lcsRec(s1Minus1, s2, lcs);
    string omits2 = lcsRec(s1, s2Minus1, lcs);

    return hash[p0] = (omits1.size() > omits2.size()) ? omits1 : omits2;
}

// NON-MEMOIZED SOLUTION
string lcsRec(string s1, string s2, string lcs = "") {
    if (s1.size() == 0 || s2.size() == 0)
        return lcs;

    string s1Minus1 = s1.substr(0, s1.size() - 1);
    string s2Minus1 = s2.substr(0, s2.size() - 1);

     if (s1[s1.size() - 1] == s2[s2.size() - 1])
        return lcsRec(s1Minus1, s2Minus1, char_to_string(s1[s1.size() - 1]) + lcs);

    string omits1 = lcsRec(s1Minus1, s2, lcs);
    string omits2 = lcsRec(s1, s2Minus1, lcs);

    return (omits1.size() > omits2.size()) ? omits1 : omits2;
}

int main() {
    // cout << lcsRec("ooappleoot", "motot") << endl;
    // hash.clear();
    // cout << lcsRec("hello", "hello") << endl;
    // hash.clear();
    cout << lcsRec("hhelloehellollohello", "hellohellok") << endl;

    // for(map< pair<string, string>, string >::iterator iter = hash.begin(); iter != hash.end(); ++iter) {
    //     cout << iter->first.first << " " << iter->first.second << " " << iter->second << endl;
    // }
}

最佳答案

这里的问题是返回值取决于lcs参数,而不仅仅是s1s2

因此 lcsRec(s1, s2, A) 将返回与 lcsRec(s1, s2, B) 不同的值(A != B),但你对它们一视同仁。

一个想法是将lcs值与返回值分开,返回值只是s1s2的LCS,忽略 lcs(然后您可能需要一个辅助调用函数将它们放在顶层)。这也许可以通过引用传递来完成,只是要小心,因为您不希望第一次调用 lcsRec(您设置 omits1)更改将在第二次调用中使用的 lcs 值(您设置 omits2 的位置)。

关于c++ - 最长公共(public)子序列算法递归解的记忆化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21011772/

相关文章:

c++ - 如何使用 operator new 创建动态二维字符数组?

c++ - constexpr 静态成员与变量

使用 C++ 的 PHP 扩展

c++ - 为什么我不能将 nullptr 转换为 weak_ptr<>

algorithm - 标记与未标记二叉树?

c++ - 这个表达式计算什么

C++ 第 3 方库包含不存在的头文件?

algorithm - 部分未知向量的最近邻

algorithm - OEIS如何进行后续检索?

c - 乘法的算法复杂度