728x90

#include <iostream>
#include <stack>
using namespace std;

int main(){
    string s;
    while(true){
        getline(cin, s);
        if(s == ".") break;
        stack<char> st;
        bool check = true;
        for(char c: s){
            if( c == '(' || c == '[' ) st.push(c);
            else if( c == ')'){
                if( !st.empty() && st.top() == '('){
                    st.pop();
                }
                else{
                    check = false;
                    break;
                }
            } else if( c == ']'){
                if( !st.empty() && st.top() == '['){
                    st.pop();
                }
                else{
                    check = false;
                    break;
                }
            }
        }
        if(check && st.empty()) cout << "yes\n";
        else cout << "no\n";
    }
}

( [ 인 경우 stack에 넣고, ) ] 경우 top이 ( [ 인지 확인한다.
만약 ) ] 경우 stack이 비어있거나 ( [ 아닌 경우 bool을 false로 바꾼다.
bool을 생각 못해서 시간이 오래걸렸던 문제다. ㅜㅜ.

반응형

+ Recent posts