Dynamic planning Brush question set Python code

Source: Internet
Author: User

1 longest common sub-sequence length

def LCS (X,y,m,n):    c = [[0]* (N+1)]* (m+1)    print (Len (c), Len (C[0])) for    I in range (m): for        J in Range (N): C4/>if X[i]==y[j]:                c[i+1][j+1]=c[i][j]+1            Else:                C[i+1][j+1]=max (c[i+1][j],c[i][j+1])    return c[m][ N]x = [1,3,2,5,6]y = [6,3,2,4,5,7,6]m,n = 5,7print (LCS (X,y,m,n))

Distortion has shortest editing distance

2 Longest ascending subsequence length

 def   lis (x,m):  if  m == 0:  return   0 c  = [1]*m  #c [i] is the longest ascending subsequence length at the end of the I subscript,   If M is not 0, then the result is at least 1  for  i in  Span style= "COLOR: #000000" > range (m):  for  J in   range (i):  if  X[i]>x[j]: c[i]  = max (C[j]+1,c[i]) return  Span style= "COLOR: #000000" > Max (c) x  = [2,1,5,7,10,4]m  = 6print  (Lis (x,m)) 

3

Dynamic planning Brush question set Python code

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.