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)