Skip to content

Commit 550fa31

Browse files
authored
Merge branch 'AlgorithmStudy-Allumbus:main' into main
2 parents 206acb2 + c549cc7 commit 550fa31

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'''
2+
BOJ #20542. 받아 쓰기 (골드3)
3+
https://www.acmicpc.net/problem/20542
4+
유형: Dynamic Programming(DP), 문자열
5+
'''
6+
7+
N, M = map(int, input().split())
8+
inp = input()
9+
answer = input()
10+
11+
dp = [[0] * (M + 1) for _ in range(N + 1)]
12+
13+
for i in range(N + 1):
14+
for j in range(M + 1):
15+
16+
if i == 0 or j == 0:
17+
if i == 0 and j == 0:
18+
continue
19+
# 빈 문자열에서 정답 문자열을 만드는 최소 편집 거리
20+
if i == 0 and j != 0:
21+
dp[i][j] = dp[i][j - 1] + 1
22+
# 빈 문자열에서 답안 문자열을 만드는 최소 편집 거리
23+
if i != 0 and j == 0:
24+
dp[i][j] = dp[i - 1][j] + 1
25+
else:
26+
# 비교 문자가 같은지 아닌지 판단
27+
if inp[i-1] == answer[j-1]:
28+
# 같으면 좌상단 대각선 값을 그대로 가져옴
29+
dp[i][j] = dp[i - 1][j - 1]
30+
else:
31+
# 비교 문자가 달라도 i나 v를 휘갈겨 쓴 경우
32+
if inp[i-1] == 'i':
33+
if answer[j-1] == 'l' or answer[j-1] == 'j':
34+
dp[i][j] = dp[i - 1][j - 1]
35+
continue
36+
elif inp[i-1] == 'v':
37+
if answer[j-1] == 'w':
38+
dp[i][j] = dp[i - 1][j - 1]
39+
continue
40+
41+
# 비교문자가 다른 경우 위, 좌측, 좌상단 대각선 값 중 최소값을 선택한 후 1을 더해준다.
42+
dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - 1], dp[i][j - 1]) + 1
43+
44+
print(dp[N][M])
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
## 🚀1월 3주차 (1/13) 스터디 발제 주제: Dynamic Programming + 문자열
2+
> 발제자: 조윤상
3+
### 🗂️ 스터디 자료
4+
- PDF: [바로가기](https://github.com/AlgorithmStudy-Allumbus/codingtest_algorithm_study/blob/main/_WeeklyChallenges/W07-%5BDP%20%2B%20String%5D/Study_BOJ_5582.pdf)
5+
6+
7+
### 📖 문제
8+
- [백준 #5582. 공통 부분 문자열](https://www.acmicpc.net/problem/5582): DP, 문자열 / 골드5
9+
- 정답 코드: [Study_BOJ_5582_공통 부분 문자열.py](https://github.com/AlgorithmStudy-Allumbus/codingtest_algorithm_study/blob/main/_WeeklyChallenges/W07-%5BDP%20%2B%20String%5D/Study_BOJ_5582_%EA%B3%B5%ED%86%B5%20%EB%B6%80%EB%B6%84%20%EB%AC%B8%EC%9E%90%EC%97%B4.py)
10+
11+
https://github.com/AlgorithmStudy-Allumbus/codingtest_algorithm_study/blob/87e6c86d66c9e3bc4abba2b468f0d09c49a66167/_WeeklyChallenges/W07-%5BDP%20%2B%20String%5D/Study_BOJ_5582_%EA%B3%B5%ED%86%B5%20%EB%B6%80%EB%B6%84%20%EB%AC%B8%EC%9E%90%EC%97%B4.py#L1-L24
12+
13+
14+
### 💻 과제
15+
- [백준 #20542. 받아 쓰기](https://www.acmicpc.net/problem/20542): DP, 문자열 / 골드3
16+
- 정답 코드: [Assignment_BOJ_20542_받아 쓰기.py](https://github.com/AlgorithmStudy-Allumbus/codingtest_algorithm_study/blob/87e6c86d66c9e3bc4abba2b468f0d09c49a66167/_WeeklyChallenges/W07-%5BDP%20%2B%20String%5D/Assignment_BOJ_20542_%EB%B0%9B%EC%95%84%20%EC%93%B0%EA%B8%B0.py)
17+
https://github.com/AlgorithmStudy-Allumbus/codingtest_algorithm_study/blob/87e6c86d66c9e3bc4abba2b468f0d09c49a66167/_WeeklyChallenges/W07-%5BDP%20%2B%20String%5D/Assignment_BOJ_20542_%EB%B0%9B%EC%95%84%20%EC%93%B0%EA%B8%B0.py#L1-L44
578 KB
Binary file not shown.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'''
2+
BOJ #5582. 공통 부분 문자열(골드5)
3+
https://www.acmicpc.net/problem/5582
4+
유형: Dynamic Programming(DP), 문자열
5+
'''
6+
7+
string1 = input()
8+
string2 = input()
9+
10+
dp = [[0] * len(string1) for _ in range(len(string2))]
11+
12+
for i in range(len(string2)):
13+
for j in range(len(string1)):
14+
# 두 문자열의 i번째 문자와 j번째 문자가 같을때
15+
if string2[i] == string1[j]:
16+
if i > 0 and j > 0:
17+
dp[i][j] = dp[i-1][j-1] + 1
18+
else:
19+
dp[i][j] = 1
20+
else:
21+
dp[i][j] = 0
22+
23+
# dp테이블에서 최댓값 찾기
24+
print(max(max(row) for row in dp))

0 commit comments

Comments
 (0)