Thief of Wealth
시간 제한메모리 제한제출정답맞은 사람정답 비율
1 초128 MB93836531939.776%


문제

The world should be finely balanced. Positive vs. negative, light vs. shadow, and left vs. right brackets. Your mission is to write a program that judges whether a string is balanced with respect to brackets so that we can observe the balance of the world.

A string that will be given to the program may have two kinds of brackets, round (“( )”) and square (“[ ]”). A string is balanced if and only if the following conditions hold.

  • For every left round bracket (“(”), there is a corresponding right round bracket (“)”) in the following part of the string.
  • For every left square bracket (“[”), there is a corresponding right square bracket (“]”) in the following part of the string.
  • For every right bracket, there is a left bracket corresponding to it.
  • Correspondences of brackets have to be one to one, that is, a single bracket never corresponds to two or more brackets.
  • For every pair of corresponding left and right brackets, the substring between them is balanced.

입력

The input consists of one or more lines, each of which being a dataset. A dataset is a string that consists of English alphabets, space characters, and two kinds of brackets, round (“( )”) and square (“[ ]”), terminated by a period. You can assume that every line has 100 characters or less. The line formed by a single period indicates the end of the input, which is not a dataset.

출력

For each dataset, output “yes” if the string is balanced, or “no” otherwise, in a line. There may not be any extra characters in the output.


문제를 보고 왜 이 정답률이 39% 밖에 안되지? 라는 생각이 들었다.


스택의 개념을 활용하여 구현해 주면 된다.

import sys
sys.stdin = open("input.txt","r")

str_temp = input()
bracket_stack = []

while str_temp != ".":

for i in str_temp:
if i == "(" or i == "[":
bracket_stack.append(i)
elif i==")":
if len(bracket_stack) == 0:
bracket_stack.append(i)
break
elif bracket_stack[-1] == "(":
bracket_stack.pop()
else:
break
elif i=="]":
if len(bracket_stack) == 0:
bracket_stack.append(i)
break
elif bracket_stack[-1] == "[":
bracket_stack.pop()
else:
break
if len(bracket_stack) == 0:
print("yes")
else:
print("no")
bracket_stack = []
str_temp = input()


'개발 > 알고리즘' 카테고리의 다른 글

python 속도 높이기 팁  (0) 2019.03.18
Codeforces Global Round 1 (A. Parity)  (0) 2019.02.08
2312 수 복원하기  (0) 2019.01.24
1074번 Z  (0) 2019.01.15
11729 하노이 탑 이동순서  (0) 2019.01.13
profile on loading

Loading...