The longest common subsequence (LCS) for dynamic planning

Source: Internet
Author: User

The longest common sub-sequence (lcs,longest Common subsequence). It is defined as a sequence s, if it is a subsequence of two or more known sequences, and is the longest of all conforming to this condition sequence, then S is called the longest common subsequence of the known sequence. The longest common substring (which requires continuous) and the longest common subsequence is different.

Set X (M) ={x (1), X (2), X (3),...., X (M)} and Y (n) ={y (1), Y (2), Y (3),...., y (N)} The longest common subsequence Z (k) ={z (1), Z (2), Z (3),...., Z (k)}

First, the original problem is decomposed into sub-problems, a known conclusion: when m or n equals 0 o'clock, K equals 0, that is, the common subsequence length is 0

When M and n are not equal to 0 o'clock, there are three cases:

(1) x (m) = = Y (n) , at which point Z (k) = x (m) = Y (n), the element belongs to the last element of the longest common subsequence of the current. At this point Z (k-1) ={X (m-1) and Y (n-1) The longest common sub-sequence}

(2) x (m)! = Y (n) , and Z (k)! = x (m), at which point the longest common subsequence of z={x (m-1) and Y (n)}

(3) x (m)! = Y (n) , and Z (k)! = Y (n), at which point the longest common subsequence of z={X (m) and Y (n-1)}

where x (m-1) ={x (1), X (2), X (3),...., x (m-1)}, Y (n-1) ={y (1), Y (2), Y (3),...., y (n-1)},z (k-1) ={z (1), Z (2), Z (3),...., Z (k-1)}

The above three steps, each of which are based on the state of the current sequence, transform the problem into a sub-problem of a known solution (the first known sub-problem is when m==0 or n==0), thus solving the current problem.

The state transfer equation of the problem is obtained and the mathematical description is as follows:

Existing two sequence X={x1,x2,x3,...xi},y={y1,y2,y3,....,yi},

Set a c[i,j]: preserve the length of the LCS of Xi and YJ

Finally, the python implementation of the algorithm:

1 #Longest common sub-sequence problem2 __author__='Ice'3 4 5 #arr_x,arr_y [0 ~ length-1]6 #Subarr_len [0,1~x_length][0,1~y_length]7 #flag [0 ~ x_length-1][0 ~ y_length]8 9 Ten deflcs_length (arr_x, arr_y): OneX_length =Len (arr_x) AY_length =Len (arr_y) -Subarr_len = [[0 forJinchRange (Y_length + 1)] forIinchRange (X_length + 1)] -flag = [[0 forJinchRange (Y_length)] forIinchrange (x_length)] the      forIinchRange (1, x_length + 1): -          forJinchRange (1, y_length + 1): -             ifArr_x[i-1] = = Arr_y[j-1]: -SUBARR_LEN[I][J] = subarr_len[i-1][j-1] + 1 +FLAG[I-1][J-1] = 1 -             elifSUBARR_LEN[I-1][J] >= subarr_len[i][j-1]: +SUBARR_LEN[I][J] = subarr_len[i-1][j] AFLAG[I-1][J-1] = 2 at             Else: -SUBARR_LEN[I][J] = subarr_len[i][j-1] -FLAG[I-1][J-1] = 3 -     return{'Subarr_length': Subarr_len[x_length][y_length],'Flag': Flag} -  -  in defLCS (arr_x, x_i, Y_j, flag, result): -     ifX_i < 0orY_j <0: to         return +     ifFlag[x_i][y_j] = = 1: -LCS (arr_x, X_i-1, y_j-1, flag, result) the result.append (arr_x[x_i]) *     elifFlag[x_i][y_j] = = 2: $LCS (arr_x, x_i-1, Y_j, flag, result)Panax Notoginseng     elifFlag[x_i][y_j] = = 3: -LCS (arr_x, X_i, y_j-1, flag, result) the  +  Aarray_x = ['a','b','C','b','D','a','b'] theArray_y = ['b','D','C','a','b','a'] +Longest_common_subsequence = [] -Lcs_info =lcs_length (array_x, array_y) $LCS (Array_x, Len (array_x)-1, Len (array_y)-1, lcs_info['Flag'], longest_common_subsequence) $ Print(longest_common_subsequence)

The longest common subsequence (LCS) for 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.