Longest common sub-sequence (LCS)

Source: Internet
Author: User

First, the longest common sub-sequence introduction

  the subsequence of a sequence is the sequence that is obtained after deleting several elements in the sequence. For example, "ABCD" and "BDF" are all sub-sequences of "ABCDEFG".

  Longest common subsequence (Longest Common subsequence, abbreviated LCS) problem : Given two sequences x and Y, the common subsequence with the largest x and y lengths is obtained. Example: x= "Abbcbde", y= "dbbcdb", LCS (x, y) = "BBCD"

Application scenario: string similarity ratio pair, gene alignment .

1. The optimal substructure principle of LCS

Make x=<x1,x2,..., xm> and Y=<y1,y2,..., yn> to two sequences, z=<z1,z2,..., zk> for any LCS of X and Y.

1. If Xm=yn, then Zk=xm=yn and Zk-1 are an LCS of Xm-1 and Yn-1.

2. If Xm≠yn, then ZK≠XM means Z is an LCS for Xm-1 and Y.

3. If Xm≠yn, then Zk≠yn means Z is an LCS of X and Yn-1.

2. Recursive type of optimal solution

  

C[I,J] Represents the length of the LCS of Xi and YJ.

Second, examples and analysis

For example, LCS that require a= "Abcbdab" and B= "Bdcaba":

Because the last "B" =? " A ", so the LCS (a, b) should originate from one of the LCS (A[:-1],B) and LCS (A,b[:-1]). It means that the last one is different, either remove the last one from a, or remove the last one from B.

  

Longest common sub-sequence (LCS)

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.