#include <bits/stdc++.h>
using namespace std;

#define forsn(i,s,n) for(int i = s; i < n; i++ )
#define forn(i,n) forsn(i,0,n)
#define sz(x) int(x.size())

typedef vector<int> vi;



void solve(string a){

    int n = sz(a);
    stack<char>pila;
    forn(i,n){
        if(a[i] == '('|| a[i] == '['|| a[i] == '{'){
            pila.push(a[i]);
        }else if(a[i] == ')'){
            if (!pila.empty() && pila.top() == '(') {
                pila.pop();
            }else{
                cout << "NO"<< '\n';
                return;
            }
        }else if(a[i] == ']'){
            if (!pila.empty() && pila.top() == '[') {
                pila.pop();
            }else{
                cout << "NO"<< '\n';
                return;
            }
        }else if(a[i] == '}'){
            if (!pila.empty() && pila.top() == '{') {
                pila.pop();
            }else{
                cout << "NO"<< '\n';
                return;
            }
        }
    }
    if(!pila.empty()){
        cout << "NO"<< '\n';
        return;
    }
    cout << "SI"<< '\n';
    return;
}



int main() {

    string a;
    while(getline(cin,a)){
        solve(a);
    }
    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: