Skip to content

Commit cb0527e

Browse files
committed
[BOJ]#9251. LCS/골드5/힌트
https://www.acmicpc.net/problem/9251
1 parent cb2ec40 commit cb0527e

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

Hongjoo/백준/lcs.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
[BOJ]#9251.LCS / DP /Gold5/ 2025.09.09
3+
https://www.acmicpc.net/problem/9251
4+
#####
5+
6+
"""
7+
import sys
8+
input = sys.stdin.readline
9+
# 1. 입력 변수 - a 문자열 / b문자열
10+
a = list(input())[:-1]
11+
b= list(input())[:-1]
12+
# LCS 행렬 0 초기화
13+
lcs = [[0]*(len(b)+1) for _ in range(len(a)+1)]
14+
15+
#2. LCS (Longest Common Subsequence) 점화식
16+
"""
17+
i. a[i] VS b[j] 문자 비교하기
18+
(1) SAME(=) -> dp[i][j] = 위쪽 대각선 +1
19+
(2) DIFF(!=) -> dp[i][j] = MAX(왼쪽 , 위쪽)
20+
21+
ii. 최종 LCS = dp[-1][-1]
22+
"""
23+
for i in range(1,len(a)+1) :
24+
for j in range(1, len(b)+1) :
25+
if a[i-1] == b[j-1]:
26+
lcs[i][j] = lcs[i-1][j-1] + 1
27+
else :
28+
lcs[i][j] = max(lcs[i-1][j] , lcs[i][j-1])
29+
30+
# print(lcs)
31+
print(lcs[-1][-1])

0 commit comments

Comments
 (0)