Longest common subsequence (only with the array DP itself constructs the longest common subsequence in O (m+n) time)

Source: Internet
Author: User
Tags first string

Algorithm class on the machine work, want to complicate.

Given 2 sequence X={X1,X2, ,xm} and y={y1,y2, ... ,yn} x and y   Improved LCS functions, not using arrays B and only with arrays c o (M+n)

The original code:

voidLcslength (Char*x,Char*y,intMintNint**c,int**b) {       intI, J;  for(i =1; I <= m; i++) c[i][0] =0;  for(i =1; I <= N; i++) c[0][i] =0;  for(i =1; I <= m; i++)           for(j =1; J <= N; J + +)          {            if(x[i]==Y[j]) {C[i][j]=c[i-1][j-1]+1; B[I][J]=1; }            Else if(c[i-1][j]>=c[i][j-1]) {C[i][j]=c[i-1][j]; B[I][J]=2; }            Else{C[i][j]=c[i][j-1]; B[I][J]=3; }         }}voidLCS (intIintJChar*x,int**b) {      if(i = =0|| j==0)return; if(b[i][j]==1) {LCS (i-1, J-1, x,b); printf ("%c", X[i]); }      Else if(b[i][j]==2) LCS (i-1, j,x,b); ElseLCS (i,j-1, x,b);}

When the final result is printed, it is no longer marked with the b[][] array. In fact, it is also very simple, in the printing function in the b[][] state judgment, and then again as a comparison between the DP.

Note that you should not use Max to ask for LCS, because if two parameters are equal, you do not know which one is selected. The code is as follows:

#include <iostream>#include<string>#include<cstring>#include<algorithm>using namespacestd;intdp[ $][ $];strings1,s2;intLen1,len2;voidPrintintIintJ//Print Path{    if(i==0|| j==0)    return ; if(s1[i-1]==s2[j-1]) {print (I-1, J-1); cout<<s1[i-1]; }    Else    {        if(dp[i-1][j]>=dp[i][j-1]) print (I-1, J); ElsePrint (I,j-1); }}intMain () { while(1) {cout<<"Please enter the first string:"<<Endl; CIN>>S1; cout<<"Please enter a second string:"<<Endl; CIN>>S2; Len1=s1.length (); Len2=s2.length ();  for(intI=0; i<=len1;i++) dp[i][0]=0;  for(intI=0; i<=len2;i++) dp[0][i]=0;  for(intI=1; i<=len1;i++)        {             for(intj=1; j<=len2;j++)            {                if(s1[i-1]==s2[j-1])//I and J are equal in a substringdp[i][j]=dp[i-1][j-1]+1; Else                {                    //Dp[i][j]=max (dp[i-1][j],dp[i][j-1]); note here                    if(dp[i-1][j]>=dp[i][j-1]) Dp[i][j]=dp[i-1][j]; ElseDp[i][j]=dp[i][j-1]; } }} cout<<"The length of the longest common sub-sequence is:"<<dp[len1][len2]<<Endl;       Print (LEN1,LEN2); cout<<Endl; }    return 0;}

Longest common subsequence (only with the array DP itself constructs the longest common subsequence in O (m+n) time)

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.