Skip to content

Commit 7b7154e

Browse files
committed
[BOJ] #15483. 최소 편집 / 골드3 / 30분 / 성공
1 parent 5c53000 commit 7b7154e

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
A = input().strip()
5+
B = input().strip()
6+
7+
# 2차원 배열 초기화: (len(A)+1) x (len(B)+1) 크기
8+
dp = [[0] * (len(B) + 1) for _ in range(len(A) + 1)]
9+
10+
# 초기값 설정: 빈 문자열에서 변환 비용
11+
for i in range(len(A)+1):
12+
dp[i][0] = i
13+
for i in range(len(B)+1):
14+
dp[0][i] = i
15+
16+
# DP 배열 채우기
17+
for i in range(1, len(A)+1):
18+
for j in range(1, len(B)+1):
19+
if A[i-1] == B[j-1]: # 문자가 같다면 교체 비용 X(왼쪽 대각선 값)
20+
dp[i][j] = dp[i-1][j-1]
21+
else: # 다르다면 왼쪽, 위쪽, 왼쪽 대각선 위 중 최소값에 중 +1
22+
dp[i][j] = min(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]) + 1
23+
24+
# 최소 편집 횟수 출력
25+
print(dp[-1][-1])

0 commit comments

Comments
 (0)