feat: added alternative python solutions.
This commit is contained in:
Binary file not shown.
Binary file not shown.
@@ -43,7 +43,7 @@
|
||||
},
|
||||
"solutions": {
|
||||
"main-ac": "ac.cpp",
|
||||
"alternative-ac": [],
|
||||
"alternative-ac": ["alternative-ac.py"],
|
||||
"wrong-answer": [],
|
||||
"time-limit": ["TLE.cpp"],
|
||||
"time-limit-or-ac": [],
|
||||
|
||||
30
longest-common-subsequence/src/alternative-ac.py
Normal file
30
longest-common-subsequence/src/alternative-ac.py
Normal file
@@ -0,0 +1,30 @@
|
||||
n, m = map(int, input().split())
|
||||
s1, s2 = input().split()
|
||||
|
||||
dp = [[0] * (m + 1) for _ in range(n + 1)]
|
||||
|
||||
for i in range(1, n + 1):
|
||||
for j in range(1, m + 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])
|
||||
|
||||
length = dp[n][m]
|
||||
|
||||
lcs = []
|
||||
i, j = n, m
|
||||
while i > 0 and j > 0:
|
||||
if s1[i - 1] == s2[j - 1]:
|
||||
lcs.append(s1[i - 1])
|
||||
i -= 1
|
||||
j -= 1
|
||||
elif dp[i - 1][j] > dp[i][j - 1]:
|
||||
i -= 1
|
||||
else:
|
||||
j -= 1
|
||||
|
||||
lcs.reverse()
|
||||
|
||||
print(length)
|
||||
print(''.join(lcs))
|
||||
Reference in New Issue
Block a user