forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6.py
More file actions
29 lines (24 loc) · 1.04 KB
/
6.py
File metadata and controls
29 lines (24 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
str1 = input()
str2 = input()
# 최소 편집 거리 계산을 위한 다이나믹 프로그래밍
def edit_dist(str1, str2):
n = len(str1)
m = len(str2)
# 이차원 테이블을 초기화
dp = [[0] * (m + 1) for _ in range(n + 1)]
for i in range(n + 1):
for j in range(m + 1):
# 문자열 A가 비어 있다면, 문자열 B로 모든 문자를 삽입
if i == 0:
dp[i][j] = j
# 문자열 B가 비어 있다면, 문자열 A로 모든 문자를 삽입
elif j == 0:
dp[i][j] = i
# 문자가 같다면, 왼쪽 위에 해당하는 수를 그대로 가져옴
elif str1[i-1] == str2[j-1]:
dp[i][j] = dp[i-1][j-1]
# 마지막 문자가 다르다면, 모든 경우의 수 중에서 최솟값 찾기
else: # 삽입, 삭제, 교체 중에서 최소 비용을 찾아 삽입
dp[i][j] = 1 + min(dp[i][j-1], dp[i-1][j], dp[i-1][j-1])
return dp[n][m]
print(edit_dist(str1, str2))