简体   繁体   中英

Memoization of the recursive solution of Longest Common Subsequence Algorithm

When I attempt to memoize the recursive solution of the Longest Common Subsequence problem, the memoized soln returns a different answer. I can't quite seem to figure out why ...

#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;
    // }
}

The problem here is that the return value depends on the lcs parameter, not just s1 and s2 .

So lcsRec(s1, s2, A) would return a different value from lcsRec(s1, s2, B) (with A != B ), yet you treat them the same.

One idea would be to separate the lcs value from the return value, the return value just being the LCS of s1 and s2 , ignoring lcs (then you may need a helper calling function to put them together at the top level). This could perhaps be done with a pass-by-reference, just be careful, as you don't want the first call to lcsRec (where you set up omits1 ) to change the lcs value that will be used in the second call (where you set up 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];
}

Uncomment the last print loop to better understand the concept. Briefly its :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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