UVA673 - Parentheses Balance

来源:互联网 发布:mac魔兽图形接口 编辑:程序博客网 时间:2024/05/20 18:53

栈,对于左括号进栈,右括号出栈。

#include <iostream>#include <string>#include <stack>#include <sstream>typedef unsigned u;using namespace std;int main(){    ios::sync_with_stdio(false);    int T;    cin>>T;    cin.get();    string s;    while(T--){        getline(cin,s);        stringstream ss(s);        ss>>s;        bool failed=0;        stack<int> q;        for(string::iterator t=s.begin(); t!=s.end(); t++){            if(*t=='('||*t=='[') q.push(*t);            else if(*t==')'&&!q.empty()&&q.top()=='(')  q.pop();            else if(*t==']'&&!q.empty()&&q.top()=='[')  q.pop();            else { failed=1; break;}        }        cout<<(!failed&&q.empty()? "Yes":"No")<<endl;    }    return 0;}


0 0