From http://blog.csdn.net/shuangde800
Question: Click to open the link
Question
3 strings A, B, and C are provided. You need to find a string d to meet the following requirements:
A) D is the subsequence of.
B) D is the subsequence of B.
C) C is the substring of D.
Returns the maximum length of D.
Note the differences between subsequences and substrings. subsequences are discontinuous and strings are continuous.
Ideas
We can see from the question that C must be a subsequence of A and B. Assume that C has only one subsequence in A and B. Let's look at the example below:
Abcdefdeg
Acebdfgh
Cf
We can see that "CF" is in the range [3, 6] of string a and in the range [2, 6] of string B (yellow background)
Because the requested C is a substring of D, other letters in the yellow interval cannot be obtained.
The longest length is equal to the longest common sub-sequence length of the red and blue areas + the length of C
The f (I, j) of the LCS algorithm is used to obtain the longest Public String Of the first I and J of the second string.
The red part can be obtained directly.
In the latter part, you only need to reverse the two strings and then find them at LCS once.
Finally, find all the subsequences of C in A and B, and enumerate them to obtain the answer.
Code