Longest common sub-sequence DP

Source: Internet
Author: User

The algorithm teacher's assignment, a DP basic problem, gives you two sequences, asking you what the longest common subsequence is, such as: (A, B) is a subsequence (a,c,d,b). Note that not the longest common substring, where a subsequence can be discontinuous.

Two for loop out, each dp[i][j] can from dp[i-1][j-1], dp[i-1][j], dp[i][j-1] Three kinds of situation update come over, take a maximum, and then the path with 123 to save down, and finally follow the path and then reverse the output line.

Sample input:

7 4
A b C b D A b
b C D b
7 6
A b C b D A b
b D C a B A
8 4
A B C D E F G H
D C B A


#include <iostream> #include <stdio.h> #include <math.h> #include <stdlib.h> #include < string> #include <string.h> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <stack>using namespace std;typedef long long ll;const int inf=0x7fffffff; const int Max_n=1009;char x[max_n],y[max_n];int m,n,ct;int dp[max_n][max_n];int B[max_n][max_n];char ans[MAX_N];void Solve () {for (int i=1;i<=m;i++) {for (int j=1;j<=n;j++) {if (X[i]==y[j]) {dp[i][j]=                dp[i-1][j-1]+1;            B[i][j]=1;                } else if (Dp[i-1][j]>=dp[i][j-1]) {dp[i][j]=dp[i-1][j];            b[i][j]=2;                } else{Dp[i][j]=dp[i][j-1];            b[i][j]=3; }}}}void go_back (int x,int y) {if (x==0| |    y==0) return;        if (b[x][y]==1) {ans[++ct]=x[x];    Return Go_back (x-1,y-1);    }if (b[x][y]==2) {return go_back (x-1,y);    } if (b[x][y]==3) {return go_back (x,y-1);    }}int Main () {printf ("Enter the length of x and y: \ n");        while (scanf ("%d%d", &m,&n)!=eof) {memset (dp,0,sizeof (DP));        Memset (b,0,sizeof (b));        printf ("Input sequence x:\n");        for (int i=1;i<=m;i++) {scanf ("%c", &x[i]);        }//GetChar ();        printf ("Input sequence y:\n");        for (int i=1;i<=n;i++) {scanf ("%c", &y[i]);        } solve ();        printf ("eldest-oldest sequence length:%d\n", Dp[m][n]);        ct=0;        Go_back (M,n);        printf ("One of the oldest sequences is: \ n");        for (int i=ct;i>=1;i--) {printf ("%c", Ans[i]);    } printf ("The length of the input x and y: \ n"); } return 0;}


Longest common sub-sequence DP

Related Article

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.