Skip to content

Commit 19f37a5

Browse files
shotestcommonsupersequence.py
A dynamic programming based C program to find length of the shortest super sequence.
1 parent a6bb6e8 commit 19f37a5

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# A dynamic programming based python program to find length of the shortest supersequence
2+
3+
# Returns length of the shortest supersequence of X and Y
4+
def superSeq(X, Y, m, n):
5+
dp = [[0] * (n + 2) for i in range(m + 2)]
6+
7+
# Fill table in bottom up manner
8+
for i in range(m + 1):
9+
for j in range(n + 1):
10+
11+
#Below steps follow above recurrence
12+
if (not i): dp[i][j] = j
13+
elif (not j): dp[i][j] = i
14+
15+
elif (X[i - 1] == Y[j - 1]):
16+
dp[i][j] = 1 + dp[i - 1][j - 1]
17+
18+
else: dp[i][j] = 1 + min(dp[i - 1][j], dp[i][j - 1])
19+
20+
return dp[m][n]
21+
22+
# Driver Code
23+
X = "AGGTAB"
24+
Y = "GXTXAYB"
25+
print("Length of the shortest supersequence is %d" % superSeq(X, Y, len(X), len(Y)))

0 commit comments

Comments
 (0)