Some time ago I studied how to find the longest continuous public subsequence and the longest continuous substring. One of the previous students asked me how to solve this problem:
The key to the problem is how to define sub-problems,
Assume that:
XM = x1 X2 X3... XM
YN = Y1 Y2 Y3... YN
1. Longest Common subsequence (not consecutive)
Defines F (m, n) as the length of the longest subsequence between xm and YN.
So F (M, 0) = f (0, m) = 0
If XM! = YN, then f (m, n) = max {f (m-1, n), F (M, n-1 )}
If XM = YN, F (m, n) = f (S-1, n-1) + 1
The problem lies in F (m, n ). Use Bottom-up dynamic programming to solve the problem according to the formula.
2. The eldest son string (must be continuous)
Defines F (m, n) as the length of the longest substring between xm and YN and ends with XM & YN. Because the requirements are continuous, when defining F, an extra requirement string ends at XM & YN.
So F (M, 0) = f (0, m) = 0
If XM! = YN, then f (m, n) = 0
If XM = YN, F (m, n) = f (S-1, n-1) + 1
Because the longest string may not end at the end of XM/YN, all possible F (p, q) | 0 <p <m, 0 <q <n, the largest F (p, q) is the solution. Use Bottom-up dynamic programming to solve the problem according to the formula.