Skip to content

Commit 7bdf288

Browse files
committed
[BOJ]#1230.. 문자열거리/골드1/실패
https://www.acmicpc.net/problem/1230
1 parent eefc145 commit 7bdf288

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

Hongjoo/백준/문자열거리.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""
2+
## [백준] #1230. 문자열거리: DP / 골드1
3+
> 문제 링크 : https://www.acmicpc.net/problem/1230
4+
"""
5+
import sys
6+
input = sys.stdin.readline
7+
# 0. 입력 변수 정의 및 초기화 하기
8+
o = list(input())
9+
l = list(input())
10+
INF = 1000
11+
if len(l) < len(o) :
12+
print(-1)
13+
exit()
14+
#1. dp 정의
15+
# dp[i][j][0] : de[i][j]로 O[:i] == L[:j]
16+
dp = [[[0,0] for j in range(len(l)+1)] for i in range(len(o)+1)]
17+
# 초기화
18+
dp[0][0] = [0,INF]
19+
for j in range(1,len(l)+1):
20+
dp[0][j][1] = 1
21+
dp[0][j][0] = INF
22+
# for i in range(len(o)+1):
23+
# dp[i][0][0] = -1
24+
25+
# #2. 점화식
26+
for i in range(len(o)) :
27+
for j in range(i+1):
28+
dp[i+1][j][0] = dp[i+1][j][1] = INF
29+
for j in range(i, len(l)):
30+
if o[i] == l[j] :
31+
dp[i+1][j+1][0] = min(dp[i][j][0] , dp[i][j][1])
32+
else :
33+
dp[i+1][j+1][0] = INF
34+
dp[i+1][j+1][1] = min(dp[i+1][j][0] +1 , dp[i+1][j][1])
35+
#3. 출력
36+
if min(dp[-1][-1][0],dp[-1][-1][1]) >= INF :
37+
print(-1)
38+
else :
39+
print(min(dp[-1][-1][0] , dp[-1][-1][1]))

0 commit comments

Comments
 (0)