Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Help] [BOJ, 4949] 균형잡힌 세상 #92

Closed
takoyummy opened this issue Mar 26, 2021 · 3 comments
Closed

[Help] [BOJ, 4949] 균형잡힌 세상 #92

takoyummy opened this issue Mar 26, 2021 · 3 comments
Labels
Help Extra attention is needed Python Programming Language

Comments

@takoyummy
Copy link
Contributor

문제

https://www.acmicpc.net/problem/4949

작성한 코드

import sys

input = sys.stdin.readline

while True:
    sentence = input().rstrip()
    stack = []
    # flag가 여전히 True이면 균형 조건에 만족한것
    # False로 바뀌면 조건 실패함
    flag = True
    # .이 탈출조건
    if sentence == '.':
        break

    for i in sentence:
        # 만약 ( 나 [ 라면 스택에 추가해라
        if i == '(' or i == '[':
            stack.append(i)
        elif i == ')':
            # 만약 stack이 비어있고, 최근에 스택에 들어온 값이 '['이라면
            if not stack or stack[-1] == '[':
                # flag의 값을 False로 하고
                flag = False
                # 빠져나와라
                break
            # 만약 stack에 최근에 넣은 값이 '('이라면 짝을 이루므로
            elif stack[-1] == '(':
                # 최근의 (을 꺼내서 없애라
                stack.pop()
        # 만약 ]라면
        elif i == ']':
            # 스택에 쌓인 값이 없거나 스택에 최근 넣은 값이 ( 라면
            if not stack or stack[-1] == '(':
                flag = False
                # 나와라
                break
            # 스택에 최근 넣은 값이 [이라면
            elif stack[-1] == '[':
                # 꺼내서 없애라
                stack.pop()
    # 만약 flag의 값이 여전히 True이고 스택이 비어있다면
    if flag == True and not stack:
        # YES를 출력해라
        print('YES')
    # 그렇지 않다면
    else:
        # NO를 출력해라
        print('NO')

코드 설명

문제에 주어진 예제 케이스는 다 통과하는데
틀렸다고 계속 뜹니다
반례가 뭘까요.
코드 설명은 주석에 써놓았습니다.

@takoyummy takoyummy added Help Extra attention is needed Python Programming Language labels Mar 26, 2021
@LastCow9000
Copy link
Contributor

print("yes")
print("no")
출력형식이 대문자가 아니라 소문자에요..

@takoyummy
Copy link
Contributor Author

print("yes")
print("no")
출력형식이 대문자가 아니라 소문자에요..

아 되게 허탈하네요....
ㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋ,,,,,
문제를 앞으로 잘 읽어봐야겠습니다
정말 감사합니다 ㅎㅎ,

@1chz
Copy link
Owner

1chz commented Mar 26, 2021

zzzzzzzzzzzzzzz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Help Extra attention is needed Python Programming Language
Projects
None yet
Development

No branches or pull requests

3 participants