JXNVCE ALGO-LOG YouJin Jung

BOJ-4949

스택을 활용한 문제

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

using namespace std;

bool check(char c1, char c2){
    if ((c1=='(' && c2==')') || (c1=='[' && c2==']')) return true;
    return false;
}

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

    string str = "";
    while(1) {
        getline(cin, str);
        if (str.length()==1 && str[0]=='.') break;
        
        stack<char> s;
        bool flag = true;
        
        for (int i=0; i<str.length(); i++) {
            if(str[i]=='(' || str[i]=='[') s.push(str[i]);
            if(str[i]==')' || str[i]==']') {
                if (!s.empty()){            
                    if(check(s.top(), str[i])) s.pop();
                    else {
                        flag = false;
                        break;
                    }
                }
                else {  
                    flag = false;
                    break;
                }
            }
        }
        if (flag==false) cout << "no" << endl;
        else {
            if(s.empty()) {
                cout << "yes" << endl;
                continue;
            }
            else cout << "no" << endl;
        }
    }    
    return 0;
}

오늘 너무 창피하고 창피하다 반성해야지 ㅜㅜ… 앞으로는 더더 열심히 할테니 제발 좋은 일들만 있게 해주세요…제발제발…🙏🏼🙏🏼🙏🏼