반응형
https://www.acmicpc.net/problem/9252
a = input()
b = input()
dp = [[0]*(len(b)+1) for _ in range(len(a)+1)]
for i in range(1, len(a)+1):
for j in range(1, len(b)+1):
if a[i-1] == b[j-1]:
dp[i][j] = dp[i-1][j-1]+1
else:
dp[i][j] = max(dp[i-1][j], dp[i][j-1])
word = ''
lcs = dp[-1][-1]
count = 0
i = len(a)
j = len(b)
while count < lcs:
if dp[i][j] == dp[i-1][j]:
i -= 1
elif dp[i][j] == dp[i][j-1]:
j -= 1
elif dp[i][j] > dp[i-1][j] and dp[i][j] > dp[i][j-1]:
word += a[i-1]
count += 1
i -= 1
j -= 1
print(lcs)
print(word[::-1])
댓글