Concept explanation
Dynamic programming is to decompose large problems into sub-problems (but not simple decomposition, sub-problems to have the same sub-structure of the solution), and synthesize sub-problem solution, to derive the solution of large problems, problem solving time will be the size of the problem is increased by the power series.
Basic method: In order to save time for repeating the same sub-problem, an array is introduced, regardless of whether they are useful for the final solution, and the solution of all sub-problems is stored in the array.
Example
The question asks the longest common character sequence of a two-character sequence to
assume that two series a[],b[], with the common eldest-son sequence z[],
must have the following properties:
(1) A and b two sequence end elements are equal (a[m] = = b [n]), then the trailing elements are removed , the new common subsequence is z to remove the trailing elements ("z[0]...z[k-1" is a[m-1] and b [n-1] The common subsequence of the two sequences).
(2) while A and b two sequence trailing elements are not equal (A[m]! = b [n]),
if z[k]!=a[m], then "Z[0]...z[k-1" is a[m-2] and b[n-1] Two sequences of the common sub-sequence
if Z[K]!=B[M], then "Z[0]...z[k-1]" for a[m-1] and B[n-2] Two series of the common sub-sequence of the
above properties can be divided sub-problem,
find the common sub-sequence AB,
if a[m] = = b [n] further solve sub-problem a[m-1] and b[ The public subsequence of N-1].
if not equal the sub-problem is to find out the common subsequence L1 (a[m-2] and b[n-1]), and then find out the common subsequence b[n-2 (A[m-1]h and L2]), taking the longer of L1 and L2 as the longest common subsequence, which is which one is long.
Programming
The introduction of two auxiliary arrays (used to construct the optimal solution)
with C[i][j] Records a[i] and b[j] The length of the LCS with
D[i][j] record c[i][j] is obtained by which sub-problem ( -1,0,1 identifies left, upper left, three directions)
pseudo-code
the sequence lengths of A and B are m,n
such as a[m] = = B[n], then the common subsequence length c[m,n] = c[m-1, n-1] + 1; (i--,j--)
such as a[m]! = b[n], then c[m,n] = Max{c[m,n -1], c[m-1, n]}; (C[i,j-1]>=c[i-1,j] j--, otherwise i--;)
Wherein, I and J as index, starting from M,n, descending cycle until I = 0,j = 0
Disclaimer: The following, quoted from the network execution code
#include <stdio.h> #include <string.h> #define MaxLen lcslength (char *x, char *y, int m, int n, int
C[][maxlen], int b[][maxlen]) {int I, J;
for (i = 0; I <= m; i++) c[i][0] = 0;
for (j = 1; J <= N; j + +) C[0][j] = 0;
for (i = 1; i<= m, i++) {for (j = 1; J <= N; j + +) {if (x[i-1] = = Y[j-1])
{C[i][j] = c[i-1][j-1] + 1;
B[I][J] = 0;
} else if (C[i-1][j] >= c[i][j-1]) {c[i][j] = c[i-1][j];
B[I][J] = 1;
} else {c[i][j] = c[i][j-1];
B[I][J] =-1;
}}}} void Printlcs (int b[][maxlen], char *x, int i, int j) {if (i = = 0 | | j = = 0) return;
if (b[i][j] = = 0) {Printlcs (b, X, i-1, j-1);
printf ("%c", x[i-1]); } else if (b[i][j] = = 1) Printlcs (b,X, I-1, J);
else Printlcs (b, X, I, j-1);
} int main (int argc, char **argv) {char X[maxlen] = {"Abcbdab"};
Char Y[maxlen] = {"Bdcaba"};
int B[maxlen][maxlen];
int C[maxlen][maxlen];
int m, n;
m = strlen (x);
n = strlen (y);
Lcslength (x, Y, M, n, c, b);
Printlcs (b, X, M, n);
return 0;
}