括號畫家(棧)

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

string s;
stack<char> stk;

bool isok(){
    int l = s.length();
    for(int i = 0; i < l; ++i){
        switch(s[i]){
        case '(':
        case '[':
        case '{':
            stk.push(s[i]);
            break;
        case ')':
            if(stk.empty() || stk.top() != '(') return 0;
            else stk.pop();
            break;
        case ']':
            if(stk.empty() || stk.top() != '[') return 0;
            else stk.pop();
            break;
        case '}':
            if(stk.empty() || stk.top() != '{') return 0;
            else stk.pop();
            break;
        }
    }
    if(stk.empty()) return 1;
    else return 0;
}

int main()
{
    cin >> s;
    cout << (isok() ? "Yes" : "No") << endl;
    system("pause");
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章