[백준/C++] 4949번 균형잡힌 세상

최대 1 분 소요

1.문제 링크

4949



2. 풀이 전 계획과 생각

  • 자료구조 큐 이용하기



3. 풀이

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

int main(){
    while(true){
        stack<char> s;
        string str; 
        getline(cin, str);
        if(str==".") break;
        else{
        	bool check=false;
            for(int i=0;i<str.size();i++){
                if(str[i]=='('){
                    s.push('(');
                }
                else if(str[i]==')'){
                    if(s.size()==0 || s.top()=='['){
                        cout<<"no"<<'\n';
                        check=true;
                        break;
                    }
                    else{
                        s.pop();
                    }
                }
                else if(str[i]=='['){
                    s.push('[');
                }
                else if(str[i]==']'){
                    if(s.size()==0 || s.top()=='('){
                        cout<<"no"<<'\n';
                        check=true;
                        break;
                    }
                    else{
                        s.pop();
                    }
                }
            }
            if(check==false){
            	if(s.size()==0) cout<<"yes"<<'\n';
            	else cout<<"no"<<'\n';
			}
        }
    }
}



4. 풀이하면서 고민했던 점

  • 없음



5. 문제를 풀고 알게된 개념 및 소감

  • 자료구조 큐에 대한 이해가 올라감.