1006 Longest common subsequence LCS (classic dynamic planning)

Source: Internet
Author: User

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 sub-sequence, 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 Zk-1∈lcs (Xm-1, Yn-1) That is, the prefix Zk-1 of z is the longest common subsequence of Xm-1 and Yn-1. At this point, the problem is attributed to the Xm-1 and Yn-1 LCS (the length of the LCS (X, Y) equals the length of the LCS (Xm-1, Yn-1) plus 1).

    • If Xm≠yn, it is also not difficult to prove with contradiction: either Z∈lcs (Xm-1, Y), or Z∈lcs (X, Yn-1). Since ZK≠XM and Zk≠yn have at least one of them to be established, if ZK≠XM has Z∈lcs (Xm-1, Y), similarly, Zk≠yn (X, Z∈lcs). At this point, the problem is attributed to Xm-1 and y LCS and X and Yn-1 LCS. The length of the LCS (x, y) is: Max{lcs (Xm-1, y), length of LCS (X, Yn-1)}.

Because the length of the LCS (Xm-1, Y) and the length of the LCS (X, Yn-1) are not independent of each other in the case of Xm≠yn: both require the length of the LCS (xm-1,yn-1). 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 (xm-1,yn-1) +1;
      • 2, LCS (Xm-1,y), LCS (x,yn-1);
      • 3, max{LCS (Xm-1, Y), LCS (X, Yn-1)}.
structure of the longest common sub-sequence

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 sub-sequences z=< z1, Z2, ..., ZK;, then:

      • If Xm=yn, then Zk=xm=yn and Zk-1 are the longest common subsequence of Xm-1 and Yn-1;
      • If Xm≠yn and ZK≠XM, then Z is the longest common subsequence of Xm-1 and y;
      • If Xm≠yn and Zk≠yn, Z is the longest common sub-sequence of x and Yn-1.
        Among them Xm-1 = < X1, x2, ..., xm-1 >,yn-1 = < Y1, y2, ..., yn-1 >,zk-1 = < Z1, Z2, ..., zk-1 >.

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[I-1,J-1] + 1 If I, J > 0 and Xi = Yi
Max (C[i, J-1],c[i-1, 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[i-1] = = B[j-1]) dp[i][j] = dp[i-1][j-1]+1;elsedp[i][j] = dp[i][j-1]>dp[i-1][j]?dp[i][j -1]:d p[i-1][j];}} i = Lena,j = Lenb;int len = Dp[lena][lenb];lcs[len] = ' + ', while (Dp[i][j]) {if (dp[i][j] = = Dp[i-1][j]) i--;else if (dp[i][ J] = = Dp[i][j-1]) j--;else Lcs[--len] = A[i-1],i--, j--;} printf ("%s\n", LCS); return 0;}

  

1006 Longest common subsequence LCS (classic dynamic planning)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.