Longest public sub-sequence and longest public continuous sub-sequence-Andrew's log-Netease blog
Longest Common subsequence and longest common continuous subsequence
2012-08-26 16:02:16| Category:Algorithm Learning| Tag: |Font SizeLargeMediumSmall Subscription
1. longest Common subsequence (not consecutive)
define F (m, n) the length of the longest subsequence between xm and YN
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 DP according to the formula.
2. longest consecutive substring (must be consecutive)
define F (m, n) it is 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
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 does not necessarily end at the end of XM/YN, therefore, all possible F (p, q) | 0