[백준] 1406번

Jeanine·2022년 3월 10일
0

baekjoon

목록 보기
13/120
post-thumbnail
post-custom-banner

💻 C++ 기반

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

✔️ Short-circuit evaluation 기억하기

#include <iostream>
#include <string>
#include <stack>

using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    cin.ignore();

    while (1)
    {
        string s;
        getline(cin, s);
        if (s == ".")
        {
            break;
        }

        bool isWrong = false;
        stack<char> braces;
        for (int i = 0; i < s.length(); i++)
        {
            if (s[i] == '(' || s[i] == '[')
            {
                braces.push(s[i]);
            }
            else if (s[i] == ')')
            {
                if (braces.empty())
                {
                    isWrong = true;
                    break;
                }
                else if (braces.top() != '(')
                {
                    isWrong = true;
                    break;
                }
                else
                {
                    braces.pop();
                }
            }
            else if (s[i] == ']')
            {
                if (braces.empty())
                {
                    isWrong = true;
                    break;
                }
                else if (braces.top() != '[')
                {
                    isWrong = true;
                    break;
                }
                else
                {
                    braces.pop();
                }
            }
        }

        if (!braces.empty())
        {
            isWrong = true;
        }

        if (isWrong)
        {
            cout << "no" << '\n';
        }
        else
        {
            cout << "yes" << '\n';
        }
    }

    return 0;
}
profile
Grow up everyday
post-custom-banner

0개의 댓글