CS/코딩테스트

백준 9494번: 균형잡힌 세상 - C++ 스택(Stack)

싹난 감자 2025. 3. 25. 13:41

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

 

#include <iostream>
#include <string>
#include <stack>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    string line;
    stack<char> brackets;
    vector<string> lines;
    bool answer = true;
    
    while (true)
    {
        getline(cin, line);

        if (line == ".") break;
        
        while (!brackets.empty()) brackets.pop();
        answer = true;

        for (char ch : line)
        {
            if (ch == '(' || ch == '[')
            {
                brackets.push(ch);
            }
            else if (ch == ')' || ch == ']')
            {
                if (brackets.empty())
                {
                    answer = false;
                    break;
                }
                
                if (( brackets.top() == '(' && ch == ')' ) || ( brackets.top() == '[' && ch == ']' ))
                {
                    brackets.pop();
                }
                else
                {
                    answer = false;
                }
            }
        }

        if (answer && brackets.empty())
        {
            lines.emplace_back("yes");
        }
        else
        {
            lines.emplace_back("no");
        }
    }


    for (string answer : lines)
    {
        cout << answer << endl;
    }

}