forked from anoopojha34/TPH
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LCS
46 lines (38 loc) · 1.05 KB
/
LCS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import sys
sys.setrecursionlimit(2500)
def lcs(x,y):
for i in range(1,m):
for j in range(1,n):
if x[i]==y[j]:
c[i][j]=c[i-1][j-1]+1
b[i][j]=1
elif c[i-1][j]>=c[i][j-1] :
c[i][j]=c[i-1][j]
b[i][j]=2
else:
c[i][j]=c[i][j-1]
b[i][j]=3
return c,b
def PRINT(b,x,i,j):
if i==0 or j==0 :
return
if b[i][j]==1 :
PRINT(b,x,i-1,j-1)
main.append(x[i])
elif b[i][j]==2 :
PRINT(b,x,i-1,j)
else:
PRINT(b,x,i,j-1)
if __name__ == "__main__":
main=[]
exp=[i for i in input().split()]
x=[""]
y=[""]
for i in range(1,len(exp[0])+1):x.append(exp[0][i-1])
for i in range(1,len(exp[1])+1):y.append(exp[1][i-1])
m,n=len(x),len(y)
c=[[0 for i in range(n) ] for j in range(m)]
b=[[0 for i in range(n) ] for j in range(m)]
lcs(x,y)
PRINT(b,x,m-1,n-1)
print(len(main))