Using C + + to realize the longest common subsequence and the longest common substring _c language

Source: Internet
Author: User
Tags array definition

I. Description of the problem

Substring should be better understood, as to what is a subsequence, here is an example: there are two female strings

Cnblogs

Belong

For example, Sequence BO, BG, LG appear in both the cnblogs and belong of the parent string and the order is consistent with the parent string, which we call the common subsequence. The longest common subsequence (longest Common subsequence, LCS), as the name suggests, refers to the longest of all the subsequence sequences. A substring is a more rigorous sequence of seeds, requiring continuous occurrence in the parent string. In the example above, the longest common subsequence is the blog (cnblogs, belong), and the longest common substring is lo (cnblogs, belong).

Second, the solution algorithm

For the parent string X=<x1,x2,⋯,xm>X=<x1,x2,⋯,xm>, Y=<y1,y2,⋯,yn>Y=<y1,y2,⋯,yn> , the LCS and the longest common substring are obtained.

Brute Force Solution

Suppose m<nm<n , for the mother string xx, we can brute force to find the 2m2m, and then in order to match in the mother string yy, the algorithm's time complexity will reach the point of magnitude O(n∗2m)O(n∗2m) . It is clear that violent solutions do not apply to such problems.

Dynamic programming

Assuming z=<z1,z2,⋯,zk>z=<z1,z2,⋯,zk> is xx and yy LCS, we observe

If Xm=ynxm=yn, then Zk=xm=ynzk=xm=yn, there is zk−1zk−1 is xm−1xm−1 and yn−1yn−1 of the LCS;

If Xm≠ynxm≠yn, then ZKZK is the XMXM and yn−1yn−1 LCS, or the xm−1xm−1 and Ynyn LCS.

Therefore, the problem of solving LCS becomes two sub problems of recursive solution. However, the recursive solution of the above, the repeated sub-problems, inefficient. The improved method is to save the intermediate state by using the space to change time, and to facilitate the calculation of the latter. This is the core idea of dynamic programming (DP).

DP Solution LCS

The c[i][j] state transition equation can be obtained by using a two-dimensional array to record x1x2⋯xix1x2⋯xi与y1y2⋯yjy1y2⋯yj the LCS length of a string.

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;<  c8/>} 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 longest Common substring

As mentioned earlier, the substring is a special subsequence, so it can also be solved by DP. Defining the storage meaning of an array it is particularly important to derive the transfer equation later, and the poor array definition leads to an unusually complex transfer equation. Considering the continuity of the substring, the two-dimensional array c[i][j] is used to record the substring with such a feature-the length of the end at the same time as the end of the string x1x2⋯xix1x2⋯xi and Y1y2⋯yjy1y2⋯yj.

Get the transfer equation:

The length of the longest common substring is max(c[i,j]), i∈{1,⋯,m},j∈{1,⋯,n}max(c[i,j]), i∈{1,⋯,m},j∈{1,⋯,n} .

Code implementation

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

Summarize

The above is the entire content of this article changed, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.

Related Article

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.