반응형
[백준] 5430 AC | deque, 구현 | Gol5 Python
https://www.acmicpc.net/problem/5430
접근
- 유형 : deque, 구현
- [문제 해석]
주어진 문자열에 주어진 연산을 수행한 결과를 출력- 양쪽에서 제거가 일어나야 하므로 데크deque 사용
- 시간제한이 넉넉하지는 않은 것 같아서
뒤집기 연산 시 문자열을 실제로 뒤집지 않고 현재 앞방향을 표시해두고 그에따라 처리하는 방향으로 풀어보겠음 - 시간 제한 : 1초
트러블 슈팅
- 입력을 깔끔하지 않게 주는 경우를 많이 겪어보지 못해서
아래 경우들 처리에 좀 시간이 걸렸다. 알아두자~
- 앞뒤의 특정 문자 제거하고 입력받기 : string = input().strip(”\n그리고제거할문자들”)
** input()은 마지막에 \n가 같이 들어오므로 strip에 \n도 포함시켜 제거해줘야 함 - 구분자 없는 문자열 한 문자씩 리스트로 입력받기 : list(string.strip())
개행문자만 제거해주고 list로 감쌈
- 앞뒤의 특정 문자 제거하고 입력받기 : string = input().strip(”\n그리고제거할문자들”)
- 리스트 뒤집는 함수도 sort랑 방식이 똑같음을 기억~
- list.reverse()
- reversed(list)
코드
from collections import deque
import sys
input = sys.stdin.readline
ans = []
for _ in range(int(input())) :
coms = list(input().strip())
# print(coms)
num_cnt = int(input())
# 빈 숫자배열이 주어지는 경우 처리
if num_cnt == 0 :
nums = deque()
_ = input()
else :
nums = deque(map(int, input().strip("\n[]").split(","))) # input()은 마지막에 \n가 같이 들어오므로 strip에 \n도 포함시켜줘야 함
# print(nums)
isReversed = False
isError = False
for com in coms :
if com == 'R' :
isReversed = not isReversed
else : # 'D'인 경우
if len(nums) <= 0 :
isError = True
break
if isReversed :
nums.pop()
else :
nums.popleft()
# error 가 난 케이스 처리
if isError :
ans.append("error")
else :
# 연산이 정상 완료된 경우 마지막 방향에 따라 결과 저장
if isReversed :
nums.reverse()
ans.append("[" + ",".join(map(str, nums)) + "]")
print("\n".join(ans))
반응형
'DSA > Algorithm' 카테고리의 다른 글
[백준] 1074 Z | Gol5 | 분할정복(divide and conquer) Python (0) | 2024.10.29 |
---|---|
[백준] 2696 치즈 | Gol4 | bfs Python (0) | 2024.08.18 |
[백준] 2869 달팽이는 올라가고싶다 | 수식 | Bro1 Python (0) | 2024.07.05 |
[백준] 12904 A와 B | greedy? | Gol5 Python (0) | 2024.07.03 |
[백준] 2812 크게 만들기 | stack, greedy? | Gol3 Python (0) | 2024.07.03 |