์•Œ๊ณ ๋ฆฌ์ฆ˜ ๐Ÿ“š/๋ฐฑ์ค€

[๋ฐฑ์ค€] 9251. LCS | ํŒŒ์ด์ฌ

leejaejae 2024. 7. 19. 15:48

๋ฐฑ์ค€ 9251. LCS - ๊ณจ๋“œV

๋ฌธ์ œ ์„ค๋ช…

LCS → ๋ชจ๋‘์˜ ๋ถ€๋ถ„ ์ˆ˜์—ด์ด ๋˜๋Š” ์ˆ˜์—ด ์ค‘ ๊ฐ€์žฅ ๊ธด ๊ฒƒ์„ ์ฐพ๋Š” ๋ฌธ์ œ..!


- ์ˆœ์„œ ์ค‘์š”ํ•œ๊ฐ€ ? → ๊ทธ๋Ÿฐ๋“ฏ..!

 


ํŒŒ์ด์ฌ ์ฝ”๋“œ

A = list(input())
B = list(input())
len_A = len(A)
len_B = len(B)
# dp = []
# dp = [[0] * (len_B + 1)] * (len_A + 1)
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])

print(dp[len_A][len_B])  # dp table ๋งจ ๋งˆ์ง€๋ง‰ ๊ฐ’ ์ถœ๋ ฅ