Skip to content

Commit f1ad46d

Browse files
committed
[BOJ]#15483_최소편집/Gold4/ 60
https://www.acmicpc.net/problem/15483
1 parent a4eef29 commit f1ad46d

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

Hongjoo/백준/최소편집.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
'''
3+
Y축 (before) -> X 축 after)
4+
5+
A[i]=B[j] 같으면 : 유지 ->대각선: dp[i,j] = dp[i-1,j-1]
6+
A[i]!=B[j]의 경우 : 교체 -> 대각선 +1
7+
8+
if len(Y) > len(X) : # 빼기 => top down +1
9+
if len(Y) < len(X) : # 추가 => +1 left-right(->)
10+
dp[i][j]= min(왼,위,대각선) +1
11+
12+
'''
13+
import sys
14+
before =[0]+ list(sys.stdin.readline())[:-1]
15+
after = [0] + list(sys.stdin.readline())[:-1]
16+
dp = [[0 for _ in range(len(after))] for k in range(len(before))]
17+
18+
# 2. 최소 편집 거리
19+
20+
for i in range(len(before)):
21+
for j in range(len(after)) :
22+
# 2-1최소 편집 거리 map 초기화
23+
if i== 0 and j>0:
24+
dp[i][j] = dp[i][j-1]+1 # i=0
25+
elif j == 0 and i > 0 :
26+
dp[i][j] = dp[i-1][j]+1
27+
# 2-2 최소 편집 거리 점화식
28+
else :
29+
b = before[i]
30+
a = after[j]
31+
32+
if a == b :#유지
33+
dp[i][j] = dp[i-1][j-1]
34+
else : # 추가 , 빼기 , 교체 중 가장 편집 거리가 적은 경우+1
35+
dp[i][j] = min(dp[i][j-1], dp[i-1][j], dp[i-1][j-1]) +1 #
36+
# print(f"-{b},{a} : {dp[i][j]} ")
37+
38+
39+
print(f"{b} -> {a} : dp[{i}{j}] : {dp[i][j]}")
40+
print("")
41+
# print(dp)
42+
43+
# print("-------------")
44+
print(dp[-1][-1])

0 commit comments

Comments
 (0)