자료구조 4

[백준] 9012번 - 괄호 (By Python)

풀이 과정 모든 괄호 문자열이 입력되었을 때, '(' 문자열과 ')' 문자열의 개수가 일치하면 됩니다. 따라서, 빈 리스트를 만들어 '(' 문자열이 입력되었을 경우 리스트에 괄호를 삽입하고, ')' 문자열이 입력되었을 경우 리스트에 미리 삽입된 '(' 괄호를 제거함으로써 균형을 맞춥니다. 만약 리스트가 비어있는 상태에서 ')' 괄호열이 입력될 경우 NO 문자열을 출력하며 반복문을 종료하며, 모든 입력이 완료되었을 때 리스트에 원소가 없으면 YES 문자열을 출력합니다. 풀이 def solution(): stack=[] words = input() for word in words: if word=='(': stack.append(word) elif word==')': if stack: stack.pop() ..

[백준] 10828번 - 스택 (By Python)

풀이 import sys N=int(sys.stdin.readline()) list=[] # 빈 리스트를 생성한다. for i in range(N): # N의 숫자만큼 word=sys.stdin.readline().split() # 문장을 읽어온다. if word[0]=='push': list.append(word[1]) elif word[0]== 'pop': if len(list)==0: print(-1) else: print(list.pop()) elif word[0]=='size': print(len(list)) elif word[0]=='empty': if len(list)==0: print(1) else: print(0) elif word[0]=='top': if len(list)==0: pri..