您的位置:首页 > 其它

UVa673 - Parentheses Balance

2017-04-03 09:39 323 查看
//UVa673 - Parentheses Balance
//已AC
#include<iostream>
#include<string>
#include<stack>
using namespace std;
int main(){
int T;
cin>>T;
getchar();	//cin.get(); 读取整数后面的第一个换行符,防止误导后面的getline();
while(T--){
string str;
stack<char>s;
getline(cin,str);	//读取包括换行符在内的一整行(但是不输出换行符??)
int ok = 1;
for(int i = 0; i < str.size(); i++){
if(str[i] == '(' || str[i] == '[') s.push(str[i]);
else if(!s.empty()&& s.top() == '(' && str[i] == ')') s.pop();
else if(!s.empty()&& s.top() == '[' && str[i] == ']') s.pop();
else ok = 0;
}
ok&&s.empty()? cout<<"Yes\n": cout<<"No\n";
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: