繁体   English   中英

最长公共子序列算法的递归解的记忆

[英]Memoization of the recursive solution of Longest Common Subsequence Algorithm

当我尝试记忆最长公共子序列问题的递归解时,记忆的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 )。

    public static int len(String s1,String s2) {
    int n=s1.length();
    int m=s2.length();

    int[][] a = new int[m][n];
    for(int i=0;i<m;i++) {
        for(int j=0;j<n;j++) {
            a[i][j]=0;
            if(s1.charAt(j)==s2.charAt(i)) {
                if(i==0 || j==0)
                    a[i][j]=1;
                else
                    a[i][j]=a[i-1][j-1]+1;
            }else {
                if(i==0 && j==0)
                    a[i][j]=0;
                else if(i==0)
                    a[i][j] = a[i][j-1];
                else if(j==0)
                    a[i][j] = a[i-1][j];
                else
                    a[i][j]=Math.max(a[i-1][j], a[i][j-1]);
            }
        }
    }

    /*for(int i=0;i<m;i++) {
        for(int j=0;j<n;j++) {
            System.out.print(a[i][j]+"  ");
        }
        System.out.println();
    }*/
    return a[m-1][n-1];
}

取消注释最后的打印循环以更好地理解该概念。 简而言之:

a[i][j]=a[i-1][j-1]+1; //  if s1[j] == s2[i]
a[i][j]=Math.max(a[i-1][j], a[i][j-1]); // otherwise

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM