The longest common subsequence and the longest common substring

Source: Internet
Author: User
Problem Description:Substring should be better understood, as to what is a subsequence, here is an example: there are two strings: Cnblogs belong such as sequence Bo, BG, LG in the parent string cnblogs and belong have appeared and the order of occurrence and the parent string consistent, we call it a common sub-sequence. The longest common subsequence (longest Common Subsequence,lcs), hence the name meaning, refers to the longest of all subsequence sequences. Substrings are a more demanding sequence of seeds that require continuous presence in the parent string. In the above example, the longest common subsequence is the blog (cn BlogS bE LoN g), the longest common substring is lo (CNB LoGs,be LoNG).
Solution AlgorithmFor the mother string x=<x1, x2, ..., xm>,y=<y1, y2, ..., yn>, ask for LCS with the longest common substring. The brute force solution assumes that m<n, for the mother string X, we can brute force to find the 2 m-sub-sequence, and then match in the parent string y, the time complexity of the algorithm will reach the point of Order O (n*2 m). Obviously, the brute force solution is not very suitable for such problems. Dynamic programming hypothesis Z=&LT;Z1, Z2, ..., Zk> is the LCS of X and Y, and we observe that if Xm=yn, then Zk=xm=yn, Zk-1 is Xm-1 and Yn-1 LCS; if Xm≠yn, ZK is the LCS of Xm and Yn-1, Or the LCS of Xm-1 and yn. Therefore, the problem of solving LCS becomes two sub-problems of recursive solution. However, in the recursive solution mentioned above, the repeated sub-problems are too many and inefficient. The improved method--use the space to change the time, save the middle state with the array, convenient later computation. This is the core idea of dynamic programming (DP).
DP Solution LCS

With a two-dimensional array c[i][j] recording the length of the X1x2...xi and Y1y2...yj LCS, the state transition equation can be obtained:


Code implementation:

public static int LCS (string str1, String str2) {
     int len1 = Str1.length ();
     int len2 = Str2.length ();
     Int[][] C = new int[len1+1][len2+1];
     for (int i = 0, I <= len1; i++) {for
          (int j = 0; J <= Len2; j + +) {
               if (i = = 0 | | j = = 0) {
                    C[i][j] = 0;
  } else if (Str1.charat (i-1) = = Str2.charat (j-1)) {
                    c[i][j] = c[i-1][j-1] + 1;
               } else {
                    c[i][j] = max (c[i-1][ J], C[i][j-1]);
     }} return c[len1][len2];
}

DP solver the longest common substring mentioned before is that a substring is a special subsequence, so it can also be resolved with DP. Defining the storage meaning of an array is especially important for subsequent derivation of the transfer equation, and poor array definitions result in unusually complex transfer equations. Considering the continuity of the substring, the two-dimensional array c[i][j] is used to record the length of a substring with such characteristics-the end of which is also the end of the string x1x2...xi and Y1y2...yj. Get the transfer equation:

The code is as follows:

public static int LCS (string str1, String str2) {
     int len1 = Str1.length ();
     int len2 = Str2.length ();
     int result = 0;//record longest common substring length
     int[][] c = new int[len1+1][len2+1];
     for (int i = 0, I <= len1; i++) {for
          (int j = 0; J <= Len2; j + +) {
               if (i = = 0 | | j = = 0) {
                    C[i][j] = 0;
  } else if (Str1.charat (i-1) = = Str2.charat (j-1)) {
                    c[i][j] = c[i-1][j-1] + 1;
                    result = Max (c[i][j], result);
               } else {
                    C[i][j] = 0;}}
     }
     return result;
}

References [1] cs2035, longest Common subsequence.
[2] First-line code farming, the classic algorithm of the daily exercise-the fourth longest common sub-series.

[3] Geeksforgeeks, Dynamic programming | Set (Longest Common Substring).


This article was reproduced from: http://www.cnblogs.com/en-heng/p/3963803.html



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.