您的位置:首页 > 其它

UVA 673 Parentheses Balance

2017-09-14 11:59 453 查看
题目链接:点击打开链接

又是括号的匹配问题

还是用栈

#include<bits/stdc++.h>
using namespace std;
stack<char> sta;
int main(){
//freopen("in.txt","r",stdin);
int n;
string str;
cin>>n;
getchar();
while(n--){
while(!sta.empty()) sta.pop();
int flag=0;
getline(cin,str);
int len=str.size();
for(int i=0;i<len;i++){
if(str[i]=='['||str[i]=='(') sta.push(str[i]);
else if(!sta.empty()&&str[i]==')'&&sta.top()=='(') sta.pop();
else if(!sta.empty()&&str[i]==']'&&sta.top()=='[') sta.pop();
else flag=1;
}
if(!flag&&sta.empty()) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: