Python uses the Backtracking Method subset tree template to obtain the longest common subsequence (LCS), pythonlcs

Source: Internet
Author: User

Python uses the Backtracking Method subset tree template to obtain the longest common subsequence (LCS), pythonlcs

This example describes how to obtain the longest common subsequence (LCS) using the subset tree template of the Backtracking Method in Python. We will share this with you for your reference. The details are as follows:

Problem

Input

Row 3: string
Row 3: String B
(The length of A and B is <= 1000)

Output

Output the longest subsequence. If there are multiple subsequences, output one at will.

Input example

Belong
Cnblogs

Output example

Blog

Analysis

Since we intend to apply the Backtracking Method to the subset tree template, We need to sacrifice the element-state space analysis method.

Take the characters in a string with a smaller length as elements and the characters in a string with a larger length as the state space. For each element, traverse its state space, other things are handed over to the pruning Function !!!

The length of x is not fixed. xi indicates the sequence number in string B.

When processing each element, if no State is selected (no character in cnblogs is selected), the program cannot proceed to the next element.

This is indeed a great deal of trouble !!! After thinking for a day, I finally came up with a solution: Expand the status space and add a status q! If the element selects status q, it is valid. However, status q is not included in solution x !!!

View an intuitive figure:

So far, enjoy it!

Code

'''Longest common subsequence' # Author: hhh5460 # Date: August 1, June 3, 2017 a = 'belong 'B = 'cnblogs' x = [] # a solution (unfixed length) xi is the serial number of characters in B X = [] # A group of solutions best_x = [] # The optimal solution best_len = 0 # The maximum length of subsequences # conflict Detection def conflict (k ): global n, x, X, a, B, best_len # if two characters are not equal if x [-1] <len (B) and a [k]! = B [x [-1]: return True # If the two characters are equal, however, if a [k] = B [x [-1] and (len (x)> = 2 and x [-1] <= x [-2]): return True # if the length of the partial decomposition is added with the remaining length of a, it is less than or equal to best_len if len (x) + (len (a)-k) <best_len: return True return False # No conflict # backtracking (recursive version) def LCS (k ): # global x, X, a, B, best_len, best_x # print (k, x) if k = len (): # if len (x)> best_len: best_len = len (x) best_x = x [:] else: for I in rang E (len (B) + 1): # traverse status space: 0 ~ Len (B)-1, tip: manually add a State len (B), which indicates that if I = len (B) is selected for modifying rows without elements ): # This status is not put into solution x. LCS (k + 1) else: x. append (I) if not conflict (k): # pruning LCS (k + 1) x. pop () # backtracking # construct the oldest sequence lcsdef get_lcs (x): global B return ''based on a solution x ''. join ([B [I] for I in x]) # test LCS (0) print (B) print (best_x) print (get_lcs (best_x ))

 

Related Article

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.