BOJ9012

김현민·2021년 1월 21일
0

Algorithm

목록 보기
10/126
post-thumbnail

BOJ9012. 괄호

문제

코드

#include <iostream>
#include <stack>

using namespace std;

int main(int argc, char const *argv[])
{

    int n;
    cin >> n;
    string str;
    for (int i = 0; i < n; i++)
    {
        cin >> str;
        stack<char> st;
        bool flag = false;
        for (int j = 0; j < str.size(); j++)
        {
            char temp = str[j];
            if (temp == '(')
            {
                st.push(temp);
            }
            else if (temp == ')')
            {
                if (st.empty() || st.top() != '(')
                {
                    flag = true;
                    break;
                }
                st.pop();
            }
        }

        if (flag == true || !st.empty())
        {
            cout << "NO" << endl;
        }
        else if (st.empty())
        {
            cout << "YES" << endl;
        }
    }

    return 0;
}
  • 4949번 문제 복습 차원에서 풀어보았음.
    똑같은문제! 😁
profile
Jr. FE Dev

0개의 댓글