#coding=utf-8
# 最长公共子序列
# s1[m] = s2[n]
# LSC(m,n) = LSC(m-1,n-1) + 1
# s1[m] != s2[n]
# LSC= max(LSC(m-1,n),LSC(m,n-1))
class Solution(object):
def findlcs(self, s1, s2):
if not s1 or not s2:
return 0
m = len(s1)
n = len(s2)
lsc = [[0 for i in range(n)] for i in range(m)]
# for i in range(m):
# if s1[i] == s2[0]:
# lsc[i][0] = 1
#
# for i in range(n):
# if s2[i] == s1[0]:
# lsc[0][i] = 1
if s1[0] == s2[0]:
lsc[0][0] = 1
for i in range(m):
for j in range(n):
if s1[i] == s2[j]:
if i-1 >=0 and j-1>=0:
lsc[i][j] = lsc[i-1][j-1]+1
else:
if i-1 >= 0:
lsc[i][j] = lsc[i-1][j]
if j-1 >= 0:
lsc[i][j] = max(lsc[i][j],lsc[i][j-1])
return lsc[m-1][n-1]
s = Solution()
s1 = "abcd"
s2 = "aebd"
print s.findlcs(s1,s2)