‘’’ about the index of the lists(or arrays) to visit elements,we should realize that when we desing or understand the process of a algorithm,we usually base on the elements themselves to calculate or mark them,we needn’t too care the index of the elements the regularity of the index counting is depends on the specific programming language when we do the code implements works,just according to the processing ! on the other hand,we reference the pseudocodes to help design the architecture of the code writing, don’t to be too careful about the specific index of the pseudocodes,if you want reference furtherly,you can calculate the gap/times the loops executed’’’
code(version 1.0)
longest_common_subsequence(x, y):
deflongest_common_subsequence(x, y):
"""return the lenth of the longest subsequence of sequence x and y
Args:
x ([list]): sequence x
y (list)): sequence y
return: the lenth of the longest subsequence,the mark of the last element of the longest_common_subsequence
before the latest one
"""
m = len(x)
n = len(y)
''' calculate the index from 0
carefully to use the list resolution expression to create two dimension list'''
b = [[""for i inrange(0, n)] for j inrange(0, m)]
c = [[0for i inrange(n+1)] for j inrange(m+1)]
# for i in range(m+1):
''' the i,j index variable is to primary for traverse the table c and b
the i=1,...,m;j=1,...n
'''
for i inrange(1, m+1):
for j inrange(1, n+1):
''' debug :index out of range '''
# print('x[i]=',x[i],'i=',i,"y[j]=",y[j],"j=",j)
''' traverse the x,y sequence
index_x=0,...,m-1,so make a offset:-1'''
if x[i-1] == y[j-1]:
c[i][j] = c[i-1][j-1]+1
b[i-1][j-1] = "diagonal"
# the compare the length of the lcs currently
# if the up element >= the left element:
elif c[i-1][j] >= c[i][j-1]:
c[i][j] = c[i-1][j]
b[i-1][j-1] = "up"
else:
c[i][j] = c[i][j-1]
b[i-1][j-1] = "left"
return c[m][n], b
print_lcs(b, x, i, j):
defprint_lcs(b, x, i, j):
"""[summary]
Args:
b (table): [description]
x (list): sequence
i (int): [from 0 count]
j (int): [from 0 count]
"""
if i == -1or j == -1:
return
if b[i][j] == "diagonal":
print_lcs(b, x, i-1, j-1)
print(x[i],end=" ")
elif b[i][j] == "up":
print_lcs(b, x, i-1, j)
else:
print_lcs(b, x, i, j-1)
invoke (main)
# x=['a', 'b', 'c', 'b', 'd', 'a', 'b']
# y=['b', 'd', 'c', 'a', 'b', 'a']
''' test data x,y: '''
x = "abcbdab"
y = "bdcaba"
''' data2: '''
x="1234"
y="sqw124_"
# print(longest_common_subsequence(x,y))
b = longest_common_subsequence(x, y)[1]
print_lcs(b, x, len(b)-1, len(b[0])-1)
code (version1.5):
length_lcs(x, y):
deflength_table_lcs(x, y):
"""return the table of the length information of subsequences(processing) of sequence x and y
Args:
x ([list]): sequence x
y (list)): sequence y
return the table of the length information of subsequences(processing) of sequence x and y
"""
m = len(x)
n = len(y)
''' calculate the index from 0
carefully to use the list resolution expression to create two dimension list'''
c = [[0for i inrange(n+1)] for j inrange(m+1)]
# for i in range(m+1):
''' the i,j index variable is to primary for traverse the table c and b
the i=1,...,m;j=1,...n
'''
for i inrange(1, m+1):
for j inrange(1, n+1):
''' debug :index out of range '''
# print('x[i]=',x[i],'i=',i,"y[j]=",y[j],"j=",j)
''' traverse the x,y sequence
index_x=0,...,m-1,so make a offset:-1'''
if x[i-1] == y[j-1]:
c[i][j] = c[i-1][j-1]+1
# the compare the length of the lcs currently
# if the up element >= the left element:
elif c[i-1][j] >= c[i][j-1]:
c[i][j] = c[i-1][j]
else:
c[i][j] = c[i][j-1]
return c
get_lcs(c,x,y,i,j,lcs_sequences_set,cs=""):
defget_lcs(c,x,y,i,j,lcs_sequences_set,cs=""):
# ''' without the table c,the sequence may not be a longest one ! '''
# ''' initailly invoke is print_lcs_single(c,x,len(c),len(c[0])) '''
#index variable i,j is primaryly to visit the table c:
"""get all largest common subsequnces without repeating
Args:
c (list): two dimension list(as a table)
x (str): sequence x
y (str): sequence y
i (int): size of the table's rows
j (int): size of the table's column
lcs_sequences_set (str): contain all lcs
cs (str, optional): accumulate the common subsequence before recursively invoke the function. Defaults to ""(empty string).
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了