We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent cb2ec40 commit cb0527eCopy full SHA for cb0527e
Hongjoo/백준/lcs.py
@@ -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