Problem description
Longest Common subsequence, abbreviated as LCS (longest common subsequence ). It is defined as a sequence S. Assume that each of them is two or more subsequences of known sequences, and all of them meet the longest of the conditional sequences, S is the longest common subsequence of known sequences.
To solve the longest common subsequence, a commonly used method is to use the exhaustive method to combine all cases. However, this is very impractical for long sequences ..
If there are two sequences, X [] = {'A', 'B', 'C', 'B', 'D', 'A', 'B '}; Y [] = {'B', 'D', 'C', 'A', 'B', 'A '};
Assuming that the scheme is exhaustive, it will be the following situation:
X: A, B, C, B, D, a, B
Y: B, D, C, A, B,
A and B ratio Ratio until the first occurrence of a in a and Y stops (red), and then starts to compare B and Y in X sequence, (The Green Part). Then, we can see that running this traversal is at the cost of N * n. If we traverse all the possibilities, it will be too costly ..
There are just some other methods used by the handler,
Dynamic Planning:
We can see that there is a certain degree of similarity in the sequence solving process. For example, suppose our sequence is X: A, B, C; Y: B, D, C; Because XC = YC, we need to look at the two elements above, that is, the ratio x: A, B; Y: B, d, because XB! = Yd, then we need to infer the maximum sequence of X: A; Y: B, d, and the maximum sequence of X: A, B; Yb, take the largest of the two, it is retained as the maximum Sequence Value of X: A, B; Y: B, d.
When analyzing the problem, we always need to grasp the problem from the overall perspective and understand the overall local relevance, instead of isolating it. We just want to deduce it from the element itself, in this way, it is difficult to clarify the rules of the problem...
Finally, I looked at the introduction to algorithms to clarify this point. I was not sure about the nature of the problem ..
The law of the longest gender sequence:
C [I] [J] = {
0, I = 0 | j = 0
C [I-1] [J-1] + 1, I, j> 0 & xi = YJ
Max (C [I-1] [J], C [I] [J-1]), I, j> 0 & Xi! = YJ
}
It is really important to clearly analyze the problem .... Grasp the essence of the problem...
The Code is as follows:
Package hello. ant; // The longest public subsequence public class aloglcs {public static void main (string [] ARGs) {char X [] = {'A', 'B ', 'C', 'B', 'D', 'A', 'B'}; char y [] = {'B', 'D', 'C', 'C ', 'A', 'B', 'A'}; int C [] [] = new int [X. length + 1] [Y. length + 1]; // ^ | ^ ~~ ~~~ /// // | ~ -------~~~ /// // | ~~ ~~ //// // 0 indicates that the value is 1 to the left, and 2 to the left. The value is int flag [] [] = new int [X. length + 1] [Y. length + 1]; // used to control the printing direction // initialize for (INT I = 0; I <Y. length + 1; I ++) {C [0] [I] = 0 ;}for (INT I = 0; I <X. length + 1; I ++) {C [I] [0] = 0 ;}for (INT I = 1; I <X. length + 1; I ++) {for (Int J = 1; j <Y. length + 1; j ++) {If (X [I-1] = Y [J-1]) {c [I] [J] = C [I-1] [J-1] + 1; flag [I] [J] = 2 ;} else {If (C [I-1] [J]> = C [I] [J-1]) {C [I] [J] = C [I-1] [J]; flag [I] [J] = 0;} else {C [I] [J] = C [I] [J-1]; flag [I] [J] = 1 ;}}} stringbuilder result = new stringbuilder (); display (x, flag, X. length, Y. length, result); system. out. println ("\ n **********"); system. out. println (result. reverse (). tostring ();} static void display (char [] X, int [] [] flag, int I, Int J, stringbuilder result) {if (I = 0 | j = 0) {return;} If (flag [I] [J] = 2) {system. out. print (X [I-1] + ""); result. append (X [I-1]); display (x, flag, I-1, J-1, result);} else if (flag [I] [J] = 1) {display (x, flag, I, J-1, result);} else if (flag [I] [J] = 0) {display (x, flag, I-1, J, result );}}}
The result is as follows:
A B C B
*********
Bcba
The red part is the real eldest son sequence...