最长公共子序列算法的递归解的存在化

时间:2014-01-09 04:46:40

标签: c++ algorithm

当我试图记住最长公共子序列问题的递归解时,memoized 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;
    // }
}

2 个答案:

答案 0 :(得分:2)

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

因此lcsRec(s1, s2, A)会从lcsRec(s1, s2, B)A != B)返回不同的值,但您对它们的处理方式相同。

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

答案 1 :(得分:0)

    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