2026/02/10 ~ 2026/02/24

import sys
input = sys.stdin.readline
n = int(input())
stack = []
for _ in range(n):
# 명령어를 공백 기준으로 나눔 ("push 1" -> ["push", "1"])
command = input().split()
if command[0] == 'push':
stack.append(command[1])
elif command[0] == 'pop':
if len(stack) == 0:
print(-1)
else:
print(stack.pop())
elif command[0] == 'size':
print(len(stack))
elif command[0] == 'empty':
if len(stack) == 0:
print(1)
else:
print(0)
elif command[0] == 'top':
if len(stack) == 0:
print(-1)
else:
print(stack[-1])
최종 개수: 열린 괄호 (와 닫힌 괄호 )의 총 개수가 같아야 한다.
순서(중요): 문자열을 왼쪽부터 읽어나갈 때, 어떤 지점에서도 닫힌 괄호가 열린 괄호보다 많아져서는 안 된다.

import sys
def check_vps(s):
stack = []
for char in s:
if char == '(':
stack.append(char)
elif char == ')':
if not stack: # 꺼낼 '('가 없다면 실패
return "NO"
stack.pop()
# 모든 검사가 끝났는데 스택에 '('가 남아있으면 실패
return "YES" if not stack else "NO"
# 입력
n = int(sys.stdin.readline())
for _ in range(n):
print(check_vps(sys.stdin.readline().strip()))
import sys
def check_vps_simple(s):
count = 0
for char in s:
if char == '(':
count += 1
else:
count -= 1
if count < 0: # 0보다 작아졌다는 건 '('보다 ')'가 먼저 나왔다는 뜻
return "NO"
return "YES" if count == 0 else "NO"
# 실행 로직은 동일

import sys
input = sys.stdin.readline
def stack_number(A):
if A != 0:
li.append(A)
else:
li.pop()
li=[]
N = int(input())
for _ in range(N):
A = int(input())
stack_number(A)
print(sum(li))
import sys
from collections import deque
input = sys.stdin.readline
N = int(input())
que = deque()
for _ in range(N):
command = input().split()
if command[0] == 'push':
que.append(command[1])
elif command[0] == 'pop':
print(que.popleft() if que else -1)
elif command[0] == 'size':
print(len(que))
elif command[0] == 'empty':
print(1 if not que else 0)
elif command[0] == 'front':
print(que[0] if que else -1)
elif command[0] == 'back':
print(que[-1] if que else -1)
from collections import deque
N = int(input())
que = deque(range(1, N + 1))
# 카드가 한 장 남을 때까지 반복
while len(que) > 1:
que.popleft() # 맨 위 카드를 버림
if que: # 카드가 남아있다면
top = que.popleft() # 그다음 카드를 꺼내서
que.append(top) # 맨 뒤에 추가 (que.rotate(-1)도 가능)
print(que[0])
from collections import deque
def solve_que():
N, K = map(int, input().split())
# 1부터 N까지의 숫자를 큐에 담습니다.
que = deque(range(1, N + 1))
result = []
while que:
# K-1번 동안 맨 앞의 원소를 뒤로 보냅니다.
for _ in range(K - 1):
que.append(que.popleft())
# K번째 원소는 완전히 제거하고 결과 리스트에 추가합니다.
result.append(que.popleft())
# 출력 형식 맞추기: <3, 6, 2, 7, 5, 1, 4>
print(f"<{', '.join(map(str, result))}>")
solve_que()
N = int(input())
S = int("".join(sorted(str(N),reverse=True)))
print(S)
import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int,input().split()))
M = int(input())
B = list(map(int,input().split()))
li=[]
for i in range(M):
if B[i] in A:
C=A.count(B[i])
li.append(C)
else:
li.append(0)
print(*li)
import sys
from collections import Counter
input = sys.stdin.readline
N = int(input())
A = list(map(int,input().split()))
M = int(input())
B = list(map(int,input().split()))
count_A=Counter(A)
li = []
for i in B:
li.append(count_A[i])
print(*li)
import sys
input = sys.stdin.readline
N, M = map(int,input().split())
listen_people = []
see_people = []
listen_see_people = []
for _ in range(N):
L = str(input())
listen_people.append(L)
for _ in range(M):
S = str(input())
see_people.append(S)
for i in listen_people:
if i in see_people:
listen_see_people.append(i)
listen_see_people.sort()
print(len(listen_see_people))
for w in listen_see_people:
print(w)
import sys
input = sys.stdin.read
data = input().split()
N = int(data[0])
M = int(data[1])
listen_people = set(data[2:2+N])
see_people = set(data[2+N:])
# 교집합 연산 (&) 사용
result = sorted(list(listen_people & see_people))
print(len(result))
for name in result:
print(name)
N = int(input())
A = [0,1,]
for i in range(N-1):
B = A[i] + A[i+1]
A.append(B)
print(A[-1])
N = int(input())
A = [0,1,]
if N == 0:
print(0)
else:
for i in range(N-1):
B = A[i] + A[i+1]
A.append(B)
print(A[-1])
N = int(input())
li = []
for i in range(N):
A,B = map(int,input().split())
li.append((A,B))
ans = []
for i in range(N):
rank = 1
for j in range(N):
# 1. 나(i)와 상대방(j)을 비교
# 2. 상대방의 몸무게와 키가 모두 나보다 큰지 확인
if li[i][0] < li[j][0] and li[i][1] < li[j][1]:
rank += 1 # 나보다 덩치 큰 사람을 만날 때마다 등수 +1
ans.append(rank)
print(*(ans))
import sys
input = sys.stdin.readline
N , M = map(int,input().split())
chess_board = []
for _ in range(N):
C = str(input().strip()) # 줄바꿈 제거
if len(C) == M:
chess_board.append(C)
min_paint = 64
for i in range(N - 7):
for j in range(M - 7):
draw_w = 0 # 'W'로 시작하는 패턴일 때 틀린 개수
draw_b = 0 # 'B'로 시작하는 패턴일 때 틀린 개수
for a in range(i, i + 8):
for b in range(j, j + 8):
if (a + b) % 2 == 0:
if chess_board[a][b] != 'W':
draw_w += 1
if chess_board[a][b] != 'B':
draw_b += 1
else:
if chess_board[a][b] != 'B':
draw_w += 1
if chess_board[a][b] != 'W':
draw_b += 1
min_paint = min(min_paint, draw_w, draw_b)
print(min_paint)
import math
def solution_fac(n):
fac = math.factorial(n) # n=10
# 문자열 뒤집기
str_fac = str(fac)[::-1] # 3628800
# 뒤에서부터 0의 개수 세기
count = 0
for char in str_fac:
if char == '0':
count += 1
else:
break
return count
try:
line = sys.stdin.read().strip()
if line:
n = int(line)
print(solution_fac(n))
except EOFError:
pass
def solution_math(n): # n = 10 | # n = 25
count = 0
while n >= 5:
count += n // 5 # 10 // 5 = 2 | 25 // 5 = 5
n //= 5 # 2 // 5 = 0 | 5 // 5 = 1
return count
# 테스트
print(solution_math(10)) # 출력: 2 | # 출력: 6
while True:
N = int(input())
if N == 0:
break
if str(N) == str(N)[::-1]:
print("yes")
else:
print("no")
import math
import sys
input = sys.stdin.readline
N = int(input())
li = []
A = math.ceil(N*0.3)//2
for _ in range(N):
M = int(input())
li.append(M)
li.sort()
B = sum((li[A:N-A]))
C = N-(A*2)
print(math.ceil(B/C))

import sys
input = sys.stdin.readline
def my_round(num):
return int(num + 0.5)
N = int(input())
if N == 0:
print(0)
else:
li = []
for _ in range(N):
li.append(int(input()))
li.sort()
A = my_round(N * 0.15)
if A > 0:
target_li = li[A : N-A]
else:
target_li = li
if not target_li:
print(0)
else:
B = sum(target_li)
C = len(target_li)
print(my_round(B / C))