Transmission Door
Description
Given two string a B, the longest common subsequence of A and B (the subsequence is not required to be contiguous).
For example, two strings are: ABCICBAAbdkscab AB is a subsequence of two strings, and ABC is also, ABCA, where ABCA is the longest subsequence of the two strings.
Input
Line 1th: String A line 2nd: string B (length <= 1000)
Output
Output the longest subsequence, if there are multiple, randomly output 1.
Sample Input
Abcicbaabdkscab
Sample Output
Abca
Ideas
Remember:
Xi=﹤x1,?,xi﹥ is the first I character (1≤i≤m) (prefix) of the x sequence
Yj=﹤y1,?,yj﹥ is the first J character (1≤j≤n) (prefix) of the y sequence
Assume Z=﹤z1,?,zk﹥∈lcs (X, Y).
If the Xm=yn (the last character is the same), it is not difficult to prove with contradiction: the character must be x and y of any of the longest common subsequence Z (set length k) The last character, that is, ZK = XM = yn and apparently Zk1∈lcs (Xm1, Yn1) That is, the prefix Zk1 of z is the longest common subsequence of Xm1 and Yn1. At this point, the problem is attributed to the Xm1 and Yn1 LCS (the length of the LCS (X, Y) equals the length of the LCS (Xm1, Yn1) plus 1).
If Xm≠yn, it is also not difficult to prove with contradiction: either Z∈lcs (Xm1, Y), or Z∈lcs (X, Yn1). Since ZK≠XM and Zk≠yn have at least one of them to be established, if ZK≠XM has Z∈lcs (Xm1, Y), similarly, Zk≠yn (X, Z∈lcs). At this point, the problem is attributed to Xm1 and y LCS and X and Yn1 LCS. The length of the LCS (x, y) is: Max{lcs (Xm1, y), length of LCS (X, Yn1)}.
Because the length of the LCS (Xm1, Y) and the length of the LCS (X, Yn1) are not independent of each other in the case of Xm≠yn: both require the length of the LCS (xm1,yn1). The two other sequences of LCS contain two sequence prefixes of LCS, so the problem has the optimal substructure properties considering the dynamic programming method.
In other words, to solve this LCS problem, you ask for three things:

 1, LCS (xm1,yn1) +1;
 2, LCS (Xm1,y), LCS (x,yn1);
 3, max{LCS (Xm1, Y), LCS (X, Yn1)}.
structure of the longest common subsequence
The structure of the longest common subsequence is represented as follows:
Set sequence x=< x1, x2, ..., xm > and y=< y1, y2, ..., yn > one of the longest common subsequences z=< z1, Z2, ..., ZK;, then:
 If Xm=yn, then Zk=xm=yn and Zk1 are the longest common subsequence of Xm1 and Yn1;
 If Xm≠yn and ZK≠XM, then Z is the longest common subsequence of Xm1 and y;
 If Xm≠yn and Zk≠yn, Z is the longest common subsequence of x and Yn1.
Among them Xm1 = < X1, x2, ..., xm1 >,yn1 = < Y1, y2, ..., yn1 >,zk1 = < Z1, Z2, ..., zk1 >.
We define C[i, J] to denote the length of the LCS of Xi and Yi. If I = 0 or j = 0, which is a sequence length of 0, the length of the LCS is 0. According to the optimal substructure property of the LCS problem, the following formula can be obtained:


0 
If I = 0 or j = 0 
C[i, J] 
= 
C[I1,J1] + 1 
If I, J > 0 and Xi = Yi 


Max (C[i, J1],c[i1, J]) 
If I, J > 0 and Xi≠yi 
#include <stdio.h> #include <string.h>const int maxn = 1005;int Dp[maxn][maxn] = {0};int main () {char a[maxn],b [Maxn],lcs[maxn];int i,j;scanf ("%s", a); scanf ("%s", b); int Lena = strlen (a), LenB = strlen (b); for (i = 1;i <= lena;i++) {f or (j = 1;j <= lenb;j++) {if (a[i1] = = B[j1]) dp[i][j] = dp[i1][j1]+1;elsedp[i][j] = dp[i][j1]>dp[i1][j]?dp[i][j 1]:d p[i1][j];}} i = Lena,j = Lenb;int len = Dp[lena][lenb];lcs[len] = ' + ', while (Dp[i][j]) {if (dp[i][j] = = Dp[i1][j]) i;else if (dp[i][ J] = = Dp[i][j1]) j;else Lcs[len] = A[i1],i, j;} printf ("%s\n", LCS); return 0;}
1006 Longest common subsequence LCS (classic dynamic planning)