def lcs(str1, str2):
m = len(str1)
n = len(str2)
c = []
for i in range(m + 1):
c.append([])
for j in range(n + 1):
c[i].append(0)
b = []
for i in range(m):
b.append([])
for j in range(n):
b[i].append(0)
for i in range(1, m + 1):
for j in range(1, n + 1):
if str1[i - 1] == str2[j - 1]:
c[i][j] = c[i - 1][j - 1] + 1
elif c[i - 1][j] > c[i][j - 1]:
c[i][j] = c[i - 1][j]
b[i - 1][j - 1] = 1
else:
c[i][j] = c[i][j - 1]
b[i - 1][j - 1] = -1
str = []
i = m - 1
j = n - 1
while i >= 0 and j >= 0:
if b[i][j] == 0:
str.append(str1[i])
i = i - 1
j = j - 1
elif b[i][j] == 1:
i = i - 1
else:
j = j - 1
str.reverse()
return str
if __name__ == '__main__':
str1 = 'SCHOOLOFCOMPUTERSCIENCEANDTECHNOLOG'
str2 = 'ANHUIUNIVERSITY'
str = lcs(str1, str2)
print list(str1)
print list(str2)
print str