Dynamic planning & Longest common substring algorithm (LCS)

Source: Internet
Author: User

The longest common substring can be the length of the longest common substring, and the length of the common substring characters and the characters are recorded, and all common substrings can be found by backtracking.

The following is a dynamic programming method for finding the longest common substring length.

1: Decision making, the final step we need to make is whether to add a[n],b[m] to the common substring sequence.

2: By 1, if the length of the longest common substring of a[1..i] and B[1..J] is expressed as dp[i][j], then you can get

(1) if a[i] = = B[j] (that is, make a decision, the a[i],b[i] are added to the common sub-string)

DP[I][J] = dp[i-1][j-1] + 1

(2) if a[i]! = B[j] (Will a[i] b[i] up to one join the common substring, the specific value is determined by dp[i-1][j] and Dp[i][j-1]

DP[I][J] = max (Dp[i-1][j],dp[i][j-1]

At this point, the first feature of dynamic programming, the optimal substructure, that is, the solution of each problem (to dp[i][j], can be converted to the optimal substructure processing (i.e. dp[i-1][j],dp[i][j-1],dp[i-1][j-1], The substrings contained in these three cases are the optimal substructure, so the solution to dp[i][j] can be solved for the length of the "current oldest string" contained in the above three cases.

3: For each dp[i][j], because each dp[i][j], need to solve dp[i-1][j-1], so you can use a matrix to record the value of each dp[i][j], the matrix is a memo

The second feature of dynamic programming is that the sub-problem is repeated, which is different from the recursive algorithm such as binary tree traversal, the node that is never traversed before the traversal, and the greedy algorithm, which calls the greedy strategy on the unchanged sub-condition every time.

4: Optimize the memo and add backtracking information

Since our problem is to ask for the oldest string, we do not care about the "current oldest string" before traversing the string of A/b.

So you can use D[I][1..N] to cover the D[I-1][1..N before D[I][1..N] is obtained]

Makes the memo from the matrix into a one-dimensional array, the spatial complexity of O (M * n), into O (min (m,n))

Add backtracking information:

The most common approach is to save the entire given prefix every time you ask for the maximum length, but that would waste a lot of storage space and might make it meaningless to optimize the memo.

can find

In the process of finding the maximum length of a substring, we know at what time (i.e. A[i]==b[j], and at this time the substring length is dp[i][j]), which character is added to the substring

Dynamic planning & Longest common substring algorithm (LCS)

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.