Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | ||||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- Thymeleaf
- 스프링 시큐리티
- 관계형 데이터베이스
- 자바
- 게시판 작성자를 아이디로
- auto-offset-reset
- 자바스크립트
- java
- springboot
- msa
- JPA
- spring security
- ORM
- Kafka
- 출처 모던 자바스크립트 Deep Dive
- 모던 자바스크립트 Deep Dive
- UUID
- 공백검사
- 유효성검사
- 자바 ORM 표준 JPA 프로그래밍
- 중복되지 않는 값 만들기
Archives
- Today
- Total
인지용
[백준] 27497 알파벳 블록 [python, 실버1, 스택] 본문
https://www.acmicpc.net/problem/27497
import sys
from collections import deque
def input():
return sys.stdin.readline().strip()
n = int(input())
dq = deque([])
orders = []
for i in range(n):
arr = input().split()
command = int(arr[0])
if command == 1:
dq.append(arr[1])
orders.append(1)
elif command == 2:
dq.appendleft(arr[1])
orders.append(0)
else:
if orders:
a = orders.pop()
if a == 1:
dq.pop()
else:
dq.popleft()
if dq:
print("".join(dq))
else:
print(0)
덱과 명령어 기록용 배열을 사용해서 해결하면 된다.
현재 문자열은 덱에, 1, 2 명령어는 배열에 넣어주면 됨.
3이 나온다면 기록용 배열에 존재하는 마지막 값을 보고 분기쳐서 앞뒤중 어디서 pop 할건지 결정.
앞뒤 자유롭게 연산이 필요하다는 것을 보고 그냥 스택을 쓰지는 않을 것 같았다.
(스택으로 사용하면 시간초과 걸림)
'알고리즘' 카테고리의 다른 글
| [백준] 2257 화학식량 [python, 실버1, 스택] (0) | 2026.01.23 |
|---|---|
| [백준] 11899 괄호 끼워넣기 [python, 실버3, 스택] (0) | 2026.01.19 |
| [백준] 4889 안정적인 문자열 [python, 실버1, 스택] (0) | 2026.01.19 |
| [백준] 2841 외계인의 기타 연주 [python, 실버1, 스택] (0) | 2026.01.16 |
| [백준] 1935 후위 표기식2 [python, 실버3, 스택] (0) | 2026.01.15 |
