Python3 LCS Maximum common sub-sequence

Source: Internet
Author: User

Throw the question:

  Assuming the string s1 = ' Bdcaba ', s2 = ' Abcbdab ', the maximum common subsequence of S1 and S2 is obtained.

Problem Analysis:

  We want to ask for the largest common subsequence of S1 and S2, and we can use C (i,j) to denote the length of the largest common subsequence of S1 (i) and S2 (j),

Suppose C (i,j) = m,

if: s1[i] and s2[j] are equal, then launch C (I,J) = C (i-1,j-1) + 1,

if: s1[i] and s2[J] are unequal, then get C (i,j) = Max (c (i,j-1), C (I-1,J))

The formula is summed up as:

Drawing a table is clearer, i.e., the number of the table is the value of the current largest common subsequence, and the arrow is how the result is derived.

For example, line 5th, column 5th, because the 5th column of C and 5th row C is equal, so that is equal to the 4th row, the value of the 4th column +1 until 2

Given this list of values, you can go back to the way, from the last value, backward according to the direction of the arrow, sequentially record each equal value (that is, the direction is the value on the left), the result is the final result of our request

Code implementation:
#-*-coding:utf-8-*-#Date: 2018/6/9 15:44#Author: Small mouse#To find the longest common sub-sequence fromNumPyImport*S1='Bdcaba'S2='Abcbdab'defval_list (S1,S2):#length of two stringsLEN_S1 = Len (S1) + 1Len_s2= Len (s2) + 1#List of directionsDirection_list = []    #generate a full 0 list of len_s2+1 row len_s1+1 columnsres =Zeros ((LEN_S2,LEN_S1)) Direction=Zeros ((LEN_S2,LEN_S1))#print (res_list)     forIinchRange (0, len_s2-1):         forJinchRange (0, Len_s1-1):            #Judging if they are equal            ifS1[J] = =S2[i]: res[i+ 1, j + 1] = Res[i, j] + 1#1 Left Top 2 Upper 3 leftDirection[i + 1, j + 1] = 1Else:                ifRes[i + 1, j] > Res[i, j + 1]: res[i+ 1, j + 1] = res[i + 1, j] Direction[i+ 1, j + 1] = 3Else: Res[i+ 1, j + 1] = res[i, j + 1] Direction[i+ 1, j + 1] = 2returnRes,directionres, Direction=val_list (S1,S2)#Direction List 1 left top 2 Upper 3 left#[0.0. 0.0. 0.0. 0.]#[0.2. 2.2. 1.3. 1.]#[0.1. 3.3. 2.1. 3.]#[0.2. 2.1. 3.2. 2.]#[0.1. 2.2. 2.1. 3.]#[0.2. 1.2. 2.2. 2.]#[0.2. 2.2. 1.2. 1.]#[0.1. 2.2. 2.1. 2.]#list of values for the maximum subsequence#[0.0. 0.0. 0.0. 0.]#[0.0. 0.0. 1.1. 1.]#[0.1. 1.1. 1.2. 2.]#[0.1. 1.2. 2.2. 2.]#[0.1. 1.2. 2.3. 3.]#[0.1. 2.2. 2.3. 3.]#[0.1. 2.2. 3.3. 4.]#[0.1. 2.2. 3.4. 4.]#To find out the result by backtracking recursionGlobalS_ress_res="'defLcs_fun (s,res_list,i,j):ifRES_LIST[I,J] = = 3: Lcs_fun (s,res_list,i,j-1)    elifRES_LIST[I,J] = = 2: Lcs_fun (s,res_list,i-1, J)elifRES_LIST[I,J] = = 1: Lcs_fun (s,res_list,i-1,j-1)        Globals_res s_res+ = S[i-1]    Else:        returnlcs_fun (S2,direction,len (S2), Len (S1))Print(S_res)

This piece is very easy to see faint, look carefully, should be able to understand

Python3 LCS Maximum common sub-sequence

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.