美文网首页
最长公共子串

最长公共子串

作者: 北雁南飞_8854 | 来源:发表于2019-01-01 20:18 被阅读0次
问题:

找出最长、连续的子字符串

思路:

遍历X、Y的所有子字符串,找出最长公共后缀,则最长公共后缀的长度就是最长公共子串的长度。

LCSSuffix[i][j] = LCSSuffix[i - 1][j - 1] + 1 (若X[i - 1] == Y[j - 1])
                          0                                     (若X[i - 1] != Y[j - 1])

代码
class LCS
{
    // Function to find Longest common substring of sequences
    // X[0..m-1] and Y[0..n-1]
    public static String LCS(String X, String Y, int m, int n)
    {
        int maxlen = 0;         // stores the max length of LCS
        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 (X.charAt(i - 1) == Y.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 X.substring(endingIndex - maxlen, endingIndex);
    }

    // main function
    public static void main(String[] args)
    {
        String X = "ABC", Y = "BABA";
        int m = X.length(), n = Y.length();

        // Find Longest common substring
        System.out.print("The Longest common substring is "
                        + LCS(X, Y, m, n));
    }
}

最长递增子序列

public static int LIS(int[] A, int i, int n, int prev) {
        if (i == n) {
            return 0;
        }

        int excludeCurrentIndex = LIS(A, i + 1, n, prev);

        int includeCurrentIndex = 0;
        if (A[i] > prev) {
            includeCurrentIndex = 1 + LIS(A, i + 1, n, A[i]);
        }

        return Integer.max(excludeCurrentIndex, includeCurrentIndex);
    }


    public static int LIS(int[] A) {
        //存储以A[i]结尾的递增子序列的最大长度.
        int[] L = new int[A.length];

        L[0] = 1;

        for (int i = 1; i < A.length; i++) {
            //从A[0..i-1]中挑出比A[i]小的元素.
            for (int j = 0; j < i; j++) {
                if (A[j] < A[i] && L[j] > L[i]) {
                    L[i] = L[j];
                }
            }

            L[i]++;
        }

        return Arrays.stream(L).max().getAsInt();
    }

打印最长递增子序列的版本

public static int[] findLCS(int[] A) {
        //存储以A[i]结尾的递增子序列的最大长度.
        int[] L = new int[A.length];

        L[0] = 1;

        for (int i = 1; i < A.length; i++) {
            //从A[0..i-1]中挑出比A[i]小的元素.
            for (int j = 0; j < i; j++) {
                if (A[j] < A[i] && L[j] > L[i]) {
                    L[i] = L[j];
                }
            }

            L[i]++;
        }

        int maxLen = 0;
        int index = 0;

        for (int i = 0; i < L.length; i++) {
            if (L[i] > maxLen) {
                maxLen = L[i];
                index = i;
            }
        }

        int[] result = new int[maxLen];

        for (int i = index; i >= 0; i--) {
            if (L[i] == maxLen) {
                maxLen--;
                result[maxLen] = A[i];
            }
        }
        return result;
    }

O(nlogn)算法复杂度的版本

定义数组outputList[A.length],outputList[i]表示所有长度为i+1的递增子序列的最小尾元素。
容易证明:outputList[]是一个递增数组。

public int findLIS2(int[] A) {
        ArrayList<Integer> outputList = new ArrayList<>();

        for (int x : A) {           
            int insertPos = lowerBound(outputList, 0, outputList.size(), x);
            if (insertPos >= lis.size()) {
                lis.add(x);
            } else {
                lis.set(insertPos, x);
            }
        }

        return lis.size();
    }

private int lowerBound(ArrayList<Integer> list, int first, int last, int target) {
    while (first != last) {

        int mid = first + (last - first) / 2;

        if (target > list.get(mid)) {
            first = mid + 1;
        } else {
            last = mid;
        }
    }

    return first;
}

相关文章

  • 最长公共子序列和最长公共子串

    最长公共子序列和最长公共子串区别 最长公共子串(Longest CommonSubstring)和最长公共子序列(...

  • 子串 子序列 总结

    最长公共子串 子串的要求比子序列严格,所以可以讨论子串的终点 最长公共子序列 DP解 递归+memo 最长公共回文...

  • LCS问题

    LCS问题包括最长公共子序列和最长公共子串,其中,最长公共子串要求必须连续。 对于二者的求解方式 最长公共子序列:...

  • 最长公共 / 对称字串

    求最长对称字串是求最长公共子串的变形.. (๑˘ ˘๑) 最长公共子串 Longest Common Subseq...

  • 字符串算法

    最长公共前缀 最长回文串 最长回文子序列 最长公共子串 反转单词顺序列 反转字符串 字符串转数字 IP-int互转

  • 06-18:刷题综合一:动态规划

    1、最长公共子串 牛客网:最长公共子串 https://www.nowcoder.com/practice/f33...

  • 每天一道算法题18

    【最长公共子序列,子串】给定两个字符串上str1 和 str2, 求两个字符的最长公共子序列和最长公共子串。 最长...

  • 两个字符串的最长公共子串

    最长公共子串(Longest Common Substring)与最长公共子序列(Longest Common S...

  • 动态规划 最长公共子串

    核心思路和最长公共子序列一样 区别在于子串必须连续 可以先看我之前这篇文章最长公共子序列问题总结 最长公共子串同样...

  • JS求最长公共子序列、最大公共子串、最大子段和

    一、最长公共子序列 二、最大公共子串 三、最大子段和 参考链接:查找两个字符串的最长公共子串的Javascript...

网友评论

      本文标题:最长公共子串

      本文链接:https://www.haomeiwen.com/subject/xzkolqtx.html