defLongest_Common_Subsequence(s1,s2): l1 = len(s1) l2 = len(s2) dp = [[0for _ in range(l2+1)] for _ in range(l1+1)] for i in range(1,l1+1): for j in range(1,l2+1): if s1[i-1]==s2[j-1]: dp[i][j] = dp[i-1][j-1]+1 else: dp[i][j] = max(dp[i-1][j],dp[i][j-1]) return dp[-1][-1]
defLongest_Common_Subsequence(s1,s2): l1 = len(s1) l2 = len(s2) dp = [[0for _ in range(l2+1)] for _ in range(l1+1)] memo = [[0for _ in range(l2+1)] for _ in range(l1+1)] for i in range(1,l1+1): for j in range(1,l2+1): if s1[i-1]==s2[j-1]: dp[i][j] = dp[i-1][j-1]+1 memo[i][j] = 1 else: if dp[i-1][j]>=dp[i][j-1]: dp[i][j] = dp[i-1][j] memo[i][j] = 3 else: dp[i][j] = dp[i][j-1] memo[i][j] = 2 x = l1 y = l2 start = (x,y) res = [] while x and y: if memo[x][y]==2: y = y-1 elif memo[x][y]==3: x = x-1 else: res = [s1[x-1]]+res x-=1 y-=1 print(''.join(res))