본문으로 바로가기
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

 

 

문제 : https://algospot.com/judge/problem/read/BRACKETS2

 

algospot.com :: BRACKETS2

Mismatched Brackets 문제 정보 문제 Best White is a mathematics graduate student at T1 University. Recently, he finished writing a paper and he decided to polish it. As he started to read it from the beginning, he realized that some of the formulas have problems:

algospot.com

 

스택을 이용해서 짝이 순서대로 맞는지 확인하면 된다.

계속 컴파일 에러가 나길래 왜지 했더니  s.empty()를 다른 곳에 붙여 넣기 해서 그랬다.. ㅜㅜ

 

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

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    
    int t, i;
    cin >> t;
    
    while(t--){
        stack<char> s;
        string str;
        cin >> str;
        
        for(i = 0 ; i < str.size(); i++){
            if(str[i] == '{' || str[i] == '[' || str[i] == '(')
                s.push(str[i]);
            else if(str[i] == '}'){
                if(s.empty() || s.top() != '{'){
                    cout << "NO\n";
                    break;
                }
                s.pop();
            }else if(str[i] == ']'){
                if(s.empty() || s.top() != '['){
                    cout << "NO\n";
                    break;
                }
                s.pop();
            }else if(str[i] == ')'){
                if(s.empty() || s.top() != '('){
                    cout << "NO\n";
                    break;
                }
                s.pop();
            }
        }
        if(i != str.size())
            continue;
        
        if(s.empty())
            cout << "YES\n";
        else
            cout << "NO\n";
    }
    return 0;
}