简体   繁体   English

寻找最长的公共子串[DP]时如何获得空间复杂度O(n)?

[英]How can I get Space complexity O(n) while looking for the longest common substring [DP]?

¡Hello! 你好!

I'm trying to find the longest common substring between two strings with a good time and space complexity, following using dynamic programming. 在尝试使用动态编程之后,我试图找到两个字符串之间最长的公共子字符串,且时间和空间复杂度很高。 I could find a solution with O(n^2) time and space complexity: 我可以找到O(n ^ 2)时空复杂度的解决方案:

public static String LCS(String s1, String s2){
    int maxlen = 0;            // stores the max length of LCS      
    int m = s1.length();
    int n = s2.length();
    int endingIndex = m;  // stores the ending index of LCS in X

    // lookup[i][j] stores the length of LCS of substring
    // X[0..i-1], Y[0..j-1]
    int[][] lookup = new int[m + 1][n + 1];

    // fill the lookup table in bottom-up manner
    for (int i = 1; i <= m; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            // if current character of X and Y matches
            if (s1.charAt(i - 1) == s2.charAt(j - 1))
            {
                lookup[i][j] = lookup[i - 1][j - 1] + 1;

                // update the maximum length and ending index
                if (lookup[i][j] > maxlen)
                {
                    maxlen = lookup[i][j];
                    endingIndex = i;
                }
            }
        }
    }

    // return Longest common substring having length maxlen
    return s1.substring(endingIndex - maxlen, endingIndex);

}

My question is: How can I get better space complexity? 我的问题是:如何获得更好的空间复杂性?

Thanks in advance! 提前致谢!

The best time complexity you can get for finding the LCS of two Strings is O(n^2) using dynamic programming. 使用动态编程,找到两个字符串的LCS可获得的最佳时间复杂度是O(n ^ 2)。 I tried to find another algorithm for the problem since it was one of my University projects. 由于这是我的大学项目之一,因此我试图找到解决该问题的另一种算法。 But the best thing I could find was an algorithm with O(n^3) complexity. 但是我能找到的最好的东西是一个O(n ^ 3)复杂度的算法。 The main solution of this problem uses "Recurrence relation" which uses less space but far more process. 该问题的主要解决方案是使用“递归关系”,该关系使用更少的空间,但需要更多的处理。 But like "Fibonacci series" computer scientists used dynamic programming to reduce the time complexity. 但是像“斐波那契系列”一样,计算机科学家使用动态编程来减少时间复杂度。 The Recurrence relation code: 重复关系代码:

void calculateLCS(string &lcs , char frstInp[] , char secInp[] , int lengthFrstInp ,  int lengthSecInp) {

if (lengthFrstInp == -1 || lengthSecInp == -1)
    return;

if (frstInp[lengthFrstInp] == secInp[lengthSecInp]) {
    lcs += frstInp[lengthFrstInp];
    lengthFrstInp--;
    lengthSecInp--;
    calculateLCS(lcs, frstInp, secInp, lengthFrstInp, lengthSecInp);

}


else {

    string lcs1 ="";
    string lcs2 ="";    
    lcs1 = lcs;
    lcs2 = lcs;
    calculateLCS(lcs1, frstInp, secInp, lengthFrstInp, lengthSecInp - 1);
    calculateLCS(lcs2, frstInp, secInp, lengthFrstInp - 1, lengthSecInp);

    if (lcs1.size() >= lcs2.size())
        lcs = lcs1;
    else
        lcs = lcs2;

}

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

相关问题 如何构建具有O(n)空间复杂度的树? - How can I build this tree with O(n) space complexity? 如何在时间 O(n) 和空间 O(1) 的情况下找到最大差异为 0 和 1 的最长子数组 - how can i find the longest subarray with a max difference of 0 and 1 with time O(n) and space O(1) 如何修改此代码以使时间复杂度为o(log n)或o(n)而不是o(n ^ 2) - how can i amend this code to have a time complexity of o(log n) or o(n) instead of o(n^2) 如何使它的空间复杂度为O(1)而不是O(n)? - How do I make this have a space complexity of O(1) instead of O(n)? O(n * logn)复杂度最长的双子序列 - Longest bitonic subsequence in O(n*logn) complexity 如何降低O(n ^ 3)的复杂度? (代码提供) - How can I cut down the complexity of O(n^3) ? (Code is provided) n个字符串的最长公共子串的Java实现 - Java implementation for longest common substring of n strings 这个函数(for loop)是空间复杂度O(1)还是O(n)? - Is this function (for loop) space complexity O(1) or O(n)? 这个子算法的空间复杂度实际上是O(n)吗? - Is the space complexity of this subset algorithm actually O(n)? 以O(1)空间复杂度对O(n log n)中的链表进行排序 - Sort a linked list in O(n log n) with O(1) space complexity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM