Skip to content

Commit bde0c95

Browse files
authored
Merge pull request #71 from 24-Algorithm/minjeong
Minjeong / 10월 3주차 / 3문제
2 parents a4eef29 + 977a2b8 commit bde0c95

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import sys
2+
3+
input = sys.stdin.readline
4+
5+
# 좋은 수열인지 검사하는 함수 (유망성 검사 함수)
6+
def is_good(sequence):
7+
length = len(sequence)
8+
for i in range(1, length // 2 + 1):
9+
# 인접한 두 부분 수열이 동일한 경우 False
10+
if sequence[-i:] == sequence[-2 * i:-i]:
11+
return False
12+
return True
13+
14+
15+
def backtracking(sequence, n):
16+
# 종료 조건: 수열의 길이가 n이면 출력하고 프로그램 종료
17+
if len(sequence) == n:
18+
print(sequence)
19+
exit()
20+
21+
for num in "123":
22+
new_sequence = sequence + num # 새로운 수를 붙여봄
23+
if is_good(new_sequence): # 좋은 수열인지 확인
24+
backtracking(new_sequence, n) # 재귀 호출로 다음 수 탐색
25+
26+
n = int(input()) # 수열의 길이
27+
backtracking("", n)
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])
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
n = int(input())
2+
if n == 1:
3+
print("*")
4+
exit()
5+
6+
# 첫번째 줄
7+
print(" " * (n-1) + "*")
8+
9+
# 가운데 줄
10+
for i in range(1, n-1):
11+
print(" " * (n - i - 1) + "*" + " " * (2 * i - 1) + "*")
12+
13+
# 마지막 줄
14+
print("*"*(2*n-1))
15+
16+
'''
17+
n = int(input())
18+
for i in range(1, n+1):
19+
if(i==1 or i==n):
20+
print(" " * (n-i) + "*" * (2*i-1))
21+
else:
22+
print(" " * (n-i) + "*" + " " * (2*(i-1)-1) + "*")
23+
'''

0 commit comments

Comments
 (0)