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

 

우선 한줄의 입력을 받은 다음에 문자열을 순회하면서 단어일때는 제외하고 시작하는 괄호일때 ( 나 [ 를 stack에 각각 push해주고 비어있지않고 짝이 맞는 괄호가 있다면 pop을 해주고 비어있다면 순회를 종료한다. 이러한 처리를 통해 yes인지 no인지 출력해줄 수 있다.

 

정답코드

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

using namespace std;

int main() {
    while (true) {
        string line;
        getline(cin, line);  // 한 줄 전체 입력

        if (line == ".") break;  // 종료 조건

        stack<char> s;
        bool isBalanced = true;  // 균형 여부 체크

        for (int i = 0; i < line.size(); i++) {
            if (line[i] == '(' || line[i] == '[') {
                s.push(line[i]);  // 여는 괄호를 스택에 넣음
            }
            else if (line[i] == ')') {
                if (!s.empty() && s.top() == '(') {
                    s.pop();  // 괄호가 짝이 맞으면 스택에서 제거
                }
                else {
                    isBalanced = false;  // 짝이 맞지 않으면 불균형
                    break;
                }
            }
            else if (line[i] == ']') {
                if (!s.empty() && s.top() == '[') {
                    s.pop();  // 대괄호가 짝이 맞으면 스택에서 제거
                }
                else {
                    isBalanced = false;
                    break;
                }
            }
        }

        if (!s.empty()) isBalanced = false;  // 스택이 비어 있지 않으면 불균형

        if (isBalanced) cout << "yes" << endl;
        else cout << "no" << endl;
    }

    return 0;
}

+ Recent posts