Longest common sub-sequence-dynamic planning

Source: Internet
Author: User

A sub-sequence of a string is a sequence of elements that are removed from the sequence, such as a helloworld of a hold.

Consider how the longest common subsequence problem is decomposed into sub-problems, set a= "A0,a1,...,am-1", b= "B0,b1,...,bm-1", and z= "Z0,z1,...,zk-1" as their longest common subsequence.

It is not difficult to prove the following properties:

(1) If am-1=bn-1, then zk-1=am-1=bn-1, and "Z0,z1,...,zk-2" is a "a0,a1,...,am-2" and "b0,b1,...,bn-2" a longest common sub-sequence;

(2) If the am-1!=bn-1, if the zk-1!=am-1, the implication "z0,z1,...,zk-1" is "a0,a1,...,am-2" and "b0,b1,...,bn-1" a longest common sub-sequence;

(3) If am-1!=bn-1, then if zk-1!=bn-1, implication "z0,z1,...,zk-1" is a "a0,a1,...,am-1" and "b0,b1,...,bn-2" a longest common sub-sequence.

Thus, when looking for the common sub-sequences of A and B, if there is a am-1=bn-1, then further solve a sub-problem, find "a0,a1,...,am-2" and "b0,b1,...,bm-2" a longest common sub-sequence;

If am-1!=bn-1, solve two sub-problems, find one of the longest common subsequence of "a0,a1,...,am-2" and "b0,b1,...,bn-1" and find a longest common subsequence of "a0,a1,...,am-1" and "B0,b1,...,bn-2", Then take the older elders as the longest common subsequence of a and B.

Define LCS[I][J] for the length of the longest common subsequence of the sequence "a0,a1,...,ai-1" and "b0,b1,...,bj-1", the calculation Lcs[i][j] can be recursively expressed as follows:

(1) Lcs[i][j] = 0 if i=0 or j=0;

(2) Lcs[i][j] = lcs[i-1][j-1]+1 if i,j>0, and a[i-1] = b[j-1];

(3) Lcs[i][j] = Max{lcs[i][j-1], Lcs[i-1][j]} if i,j>0, and a[i-1]! = b[j-1].

By this calculation, you can write out the length function that calculates the longest common subsequence of two series. Because the generation of lcs[i][j] depends only on lcs[i-1][j-1], lcs[i-1][j] and lcs[i][j-1], it is possible to start with lcs[m][n], and then construct the longest common sub-sequence in reverse. See the procedure for details.

1  PackageLCS;2 3  Public classLCS {4     Private Static voidcomsubstr (String str1,string str2) {5         Char[] A=Str1.tochararray ();6         Char[] b=Str2.tochararray ();7         intalen=a.length;8         intblen=b.length;9         int[] lcs=New int[Alen+1] [Blen+1];Ten          One          for(inti=0;i<=alen;i++) A              for(intj=0;j<=blen;j++) -Lcs[i][j]=0; -          the          for(inti=1;i<=alen;i++) -              for(intj=1;j<=blen;j++) -             { -                 if(a[i-1]==b[j-1]) +Lcs[i][j]=lcs[i-1][j-1]+1; -                 Else { +Lcs[i][j]=lcs[i][j-1]>lcs[i-1][j]?lcs[i][j-1]:lcs[i-1][j]; A                 } at             } -          -          for(inti=0;i<=alen;i++) -         { -              for(intj=0;j<=blen;j++) -             { inSystem.out.print (lcs[i][j]+ ","); -             } to System.out.println (); +         } -          the         intmaxlen=Lcs[alen][blen]; *         intI=ALen; $         intj=Blen;Panax Notoginseng         Char[] comsubstr=New Char[MaxLen]; -          while(maxlen>0) the         { +             if(Lcs[i][j]!=lcs[i-1][j-1])//stating that ai-1 or bj-1 are public in a0......ai-1,b0.......bj-1; A             { the                 if(Lcs[i][j-1]==lcs[i-1][j])//Description Ai-1=bj-1 is a public character; +                 { -Comsubstr[maxlen-1]=a[i-1]; $maxlen--; $i--; -j--; -                 } the                 Else{//take the longer of the two common sub-sequences as a, B, -                     if(lcs[i][j-1]>lcs[i-1][j])Wuyij--; the                     Else { -i--; Wu                     } -                 } About             } $             Else{ -i--; -j--; -             } A         } +System.out.print ("The longest common string is:"); the System.out.println (COMSUBSTR); -     } $      Public Static voidMain (string[] args) { theString a = "blog.csdn.net";  theString B = "Csdn.blogt"; the Lcs.comsubstr (A, b); the     } -}

The output is:

0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,1,2,2,2,2,0,0,0,0,0,0,1,2,3,3,3,0,0,0,0,0,0,1,2,3,4,4,0,0,0,0,0,1 , 1,2,3,4,4,0,1,1,1,1,1,1,2,3,4,4,0,1,2,2,2,2,2,2,3,4,4,0,1,2,3,3,3,3,3,3,4,4,0,1,2,3,4,4,4,4,4,4,4,0,1,2,3,4,5,5,5,5,5,5 , 0,1,2,3,4,5,5,5,5,5,5,0,1,2,3,4,5,5,5,5,5,5,0,1,2,3,4,5,5,5,5,5,6, the longest common string is: csdn.t

  

Longest common sub-sequence-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.