
푼 날짜 : 2024.12.09
푼 문제 : [5430] / AC
사용한 언어 : python
알고리즘 : 문자열, 파싱, 덱
접근방식 :
처음에는 배열을 수행 값 마다 변경해서 시간 초과가 났다.
따라서 인덱스 위치와 방향 값만 체크 후 그에 따라 출력을 해주었다.
코드 :
import sys
input = sys.stdin.readline
T = int(input())
for _ in range(T):
p_list = list(input().rstrip())
n = int(input())
n_list = list(input().rstrip())
lst = []
# 문자열로 받은 숫자 처리를 위함
now_str = ''
for i in n_list:
if i == '[' or i == ',' or i == ']':
if len(now_str):
lst.append(now_str)
now_str = ''
else:
now_str += i
# 방향 설정을 위한 변수
now = True
# 추후 답 출력을 위한 변수
ans = ''
# 인덱스 확인을 위한 변수
start, end = 0, len(lst)
for i in p_list:
# R은 방향 바꾸기
if i == 'R':
now = not now
# 삭제 해야하는데 start 인덱스가 end 보다 커졌을 경우 에러 처리
elif i == 'D' and start>=end:
ans = 'error'
break
# 삭제, 방향이 True면 start 인덱스 증가
elif i == 'D' and now:
start+=1
# 삭제, 방향이 False이면 end 인덱스 감소
elif i == 'D' and not now:
end-=1
# ans가 에러거나 start 인덱스가 end 보다 커졌을 경우
if ans == "error" or start > end:
print('error')
# 에러없이 start 인덱스와 end 인덱스가 같을 경우 빈 배열 출력
elif start == end:
print('[]')
# now 가 True면 앞에서부터 하나씩 출력
elif now:
ans = '['
for i in range(start, end):
ans+=lst[i]
ans+=','
ans = ans[:-1]+']'
print(ans)
# now 가 False면 뒤에서부터 하나씩 출력
else:
ans = '['
for i in range(end-1, start-1, -1):
ans+=lst[i]
ans+=','
ans = ans[:-1]+']'
print(ans)
골드라 어려울 줄 알았는데 사실 조금 많이 귀찮은 구현 문제였다!
모든 경우의 수를 생각하고 제대로 파싱하는게 중요했던 문제!
'Programming > Algorithm' 카테고리의 다른 글
[백준22116] / 창영이와 퇴근 - 다익스트라/최단 경로 (1) | 2024.12.20 |
---|---|
[백준5568] / 카드 놓기 - 백트래킹 (0) | 2024.12.10 |
[백준14620] / 꽃길 - 브루트포스, 백트래킹 (0) | 2024.12.05 |
[백준26169] / 세 번 이내에 사과를 먹자 - DFS,백트래킹 (1) | 2024.12.04 |
[백준2665] / 미로만들기 - 다익스트라/그래프탐색 (1) | 2024.11.20 |

푼 날짜 : 2024.12.09
푼 문제 : [5430] / AC
사용한 언어 : python
알고리즘 : 문자열, 파싱, 덱
접근방식 :
처음에는 배열을 수행 값 마다 변경해서 시간 초과가 났다.
따라서 인덱스 위치와 방향 값만 체크 후 그에 따라 출력을 해주었다.
코드 :
import sys
input = sys.stdin.readline
T = int(input())
for _ in range(T):
p_list = list(input().rstrip())
n = int(input())
n_list = list(input().rstrip())
lst = []
# 문자열로 받은 숫자 처리를 위함
now_str = ''
for i in n_list:
if i == '[' or i == ',' or i == ']':
if len(now_str):
lst.append(now_str)
now_str = ''
else:
now_str += i
# 방향 설정을 위한 변수
now = True
# 추후 답 출력을 위한 변수
ans = ''
# 인덱스 확인을 위한 변수
start, end = 0, len(lst)
for i in p_list:
# R은 방향 바꾸기
if i == 'R':
now = not now
# 삭제 해야하는데 start 인덱스가 end 보다 커졌을 경우 에러 처리
elif i == 'D' and start>=end:
ans = 'error'
break
# 삭제, 방향이 True면 start 인덱스 증가
elif i == 'D' and now:
start+=1
# 삭제, 방향이 False이면 end 인덱스 감소
elif i == 'D' and not now:
end-=1
# ans가 에러거나 start 인덱스가 end 보다 커졌을 경우
if ans == "error" or start > end:
print('error')
# 에러없이 start 인덱스와 end 인덱스가 같을 경우 빈 배열 출력
elif start == end:
print('[]')
# now 가 True면 앞에서부터 하나씩 출력
elif now:
ans = '['
for i in range(start, end):
ans+=lst[i]
ans+=','
ans = ans[:-1]+']'
print(ans)
# now 가 False면 뒤에서부터 하나씩 출력
else:
ans = '['
for i in range(end-1, start-1, -1):
ans+=lst[i]
ans+=','
ans = ans[:-1]+']'
print(ans)
골드라 어려울 줄 알았는데 사실 조금 많이 귀찮은 구현 문제였다!
모든 경우의 수를 생각하고 제대로 파싱하는게 중요했던 문제!
'Programming > Algorithm' 카테고리의 다른 글
[백준22116] / 창영이와 퇴근 - 다익스트라/최단 경로 (1) | 2024.12.20 |
---|---|
[백준5568] / 카드 놓기 - 백트래킹 (0) | 2024.12.10 |
[백준14620] / 꽃길 - 브루트포스, 백트래킹 (0) | 2024.12.05 |
[백준26169] / 세 번 이내에 사과를 먹자 - DFS,백트래킹 (1) | 2024.12.04 |
[백준2665] / 미로만들기 - 다익스트라/그래프탐색 (1) | 2024.11.20 |