[백준] 10828, 9093, 9012, 1874, 1406 (파이썬)

Colacan·2022년 1월 23일
1

[백준]

목록 보기
7/43

주말의 마무리로 열심히 달려보았다. 이 파트부터는 자료구조에 관련된 내용이 시작되는 것 같았다. 스택, 팝과 같은 개념들을 위주로 문제를 풀어야했다. 파이썬의 경우 스택구조가 없어서 list를 이용하여 구현하는 것을 목표로 잡았다. 추가적으로 오늘 백준랭크 실버3을 달성했다. 1차 목표인 골드까지 열심히 해봐야겠다.

백준 10828번 스택

#파이썬은 스택구조가 없으므로 list로 구현
import sys
N = int(sys.stdin.readline())
cmd_lst = []
for i in range(N):
    word = sys.stdin.readline().split() # input 이용할시 시간초과
    cmd = word[0]
    if cmd == 'push':
        cmd_lst.append(word[1])
    elif cmd == 'pop':
        if len(cmd_lst)==0:
            print(-1)
        else:
            print(cmd_lst.pop())
    elif cmd == 'size':
        print(len(cmd_lst))
    elif cmd == 'empty':
        if len(cmd_lst)==0:
            print(1)
        else:
            print(0)
    elif cmd == 'top':
        if len(cmd_lst)==0:
            print(-1)
        else:
            print(cmd_lst[-1])

백준 9093번 단어 뒤집기

import sys
N = int(sys.stdin.readline())
for i in range(N):
    word = sys.stdin.readline().split()
    rev_word = []
    for i in word:
        for j in i:
            rev_word.append(j)
        for k in i:
            print(rev_word.pop(),end='')
        print(' ',end='')
    print('')

백준 9012번 괄호

import sys
N = int(sys.stdin.readline())
for i in range(N):
    VPS = sys.stdin.readline()
    VPS_lst = list(VPS)
    count = 0
    for i in VPS_lst:
        if i == '(':
            count+=1
        elif i == ')':
            count-=1
        if count<0:
            break
    if count==0:
        print('YES')
    else:
        print('NO')

백준 1874번 스택 수열

# 문제 이해하는데 시간이 좀 걸렸다. 더 체계적으로 생각해보자
import sys
N = int(sys.stdin.readline())
stack = []
num_lst = []
pp_lst = []
pos = True
count = 1
for i in range(N):
    num = int(sys.stdin.readline())
    while count<=num:
        stack.append(count)
        pp_lst.append('+')
        count+=1
    if stack[-1] == num:
        stack.pop()
        pp_lst.append('-')
    else:
        print('NO')
        pos = False
        break
if pos==True:
    for i in pp_lst:
        print(i)

백준 1406번 에디터

'''
# 시간초과 O(n)
import sys
Msg = sys.stdin.readline().strip()
N = int(sys.stdin.readline())
lst = list(Msg)
cursor = len(Msg)
for i in range(N):
    cmd = sys.stdin.readline().rstrip().split()
    if cmd[0] == 'L':
        if cursor !=0:
            cursor-=1
    elif cmd[0] == 'D':
        if cursor !=len(lst):
            cursor+=1
    elif cmd[0] == 'B':
        if cursor !=0:
            del lst[cursor-1]
            cursor-=1
    elif cmd[0] == 'P':
        plus = cmd[1]
        lst.insert(cursor,plus)
        cursor+=1
j=0
full = ''
while j < len(lst):
    full += lst[j]
    j += 1
print(full)
'''
# list 2개로 pop 이용
import sys
lst1 = list(sys.stdin.readline().strip())
lst2 = [] # 커서 기준 오른쪽
N = int(sys.stdin.readline())
n = len(lst1)
for i in range(N):
    cmd = sys.stdin.readline().strip()
    if cmd[0] == 'P':
        lst1.append(cmd[2])
    elif cmd[0] == 'L' and len(lst1)!=0:
        lst2.append(lst1.pop())
    elif cmd[0] == 'D' and len(lst2)!=0:
        lst1.append(lst2.pop())
    elif cmd[0] == 'B' and len(lst1)!=0:
        lst1.pop()
print(''.join(lst1+list(reversed(lst2))))
profile
For DE, DA / There is no royal road to learning

0개의 댓글