括号匹配(栈的应用)

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

int main()
{
    int n;cin>>n;
    while(n--){
        stack<char> s;
        string str;cin>>str;
        for(int i=0;i!=str.length();i++){
            if(s.empty()) s.push(str[i]);
            else{
                if(s.top()=='('){
                    if(str[i]==')') s.pop();
                    else s.push(str[i]);}
                else if(s.top()=='['){
                    if(str[i]==']') s.pop();
                    else s.push(str[i]);
                }
                else {s.push(str[i]);}
            }
        }
        if(s.empty()) cout<<"Yes"<<endl;
        else cout<<"No"<<endl;

    }
    return 0;
}

 

posted @ 2014-05-06 21:06  xzenith  阅读(184)  评论(0编辑  收藏  举报