参考:手写代码必备手册
__author__ = 'Administrator'
def lcs(x,y):
d = [0] * (len(x) + 1)
for i in range(0,len(d)):
d[i] = [0] * (len(y) + 1)
for i in range(1,len(x) + 1):
for j in range(1, len(y) + 1):
if x[i-1] == y[j-1]:
d[i][j] = d[i-1][j-1] + 1
else:
d[i][j] = max(d[i-1][j],d[i][j-1])
print d
def lcs_extend(x,y):
d = [0] * (len(x) + 1)
p = [0] * (len(x) + 1)
for i in range(0,len(d)):
d[i] = [0] * (len(y) + 1)
p[i] = [0] * (len(y) + 1)
for i in range(1,len(x) + 1):
for j in range(1, len(y) + 1):
if x[i-1] == y[j-1]:
d[i][j] = d[i-1][j-1] + 1
p[i][j] = 1
elif d[i-1][j] > d[i][j-1]:
d[i][j] = d[i-1][j]
p[i][j] = 2
else:
d[i][j] = d[i][j-1]
p[i][j] = 3
print d
print p
lcs_print(x,y,len(x),len(y),p)
def lcs_print(x,y,lenX,lenY,p):
if lenX == 0 or lenY == 0:
return
if p[lenX][lenY] == 1:
lcs_print(x,y,lenX-1,lenY-1,p)
print x[lenX-1],
elif p[lenX][lenY] == 2:
lcs_print(x,y,lenX-1,lenY,p)
else:
lcs_print(x,y,lenX,lenY-1,p)
x = 'abcdf'
y = 'facefff'
lcs_extend(x,y)