您的位置:首页 > 其它

UVa-673 Parentheses Balance(栈模拟)

2017-08-29 22:12 435 查看
简单括号匹配问题

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

stack<char> st;
char arr[130];

int main(){
int T;
cin>>T;
getchar();
while(T--){
while(st.size()) st.pop();
gets(arr);
int len = strlen(arr);
for(int i = 0; i<len; i++){
if(arr[i] == ' '){
continue;
}
if(st.size()){
if(st.top() == '(' && arr[i] == ')') st.pop();
else if(st.top() == '[' && arr[i] == ']') st.pop();
else st.push(arr[i]);
}
else st.push(arr[i]);
}
if(st.empty()) printf("Yes\n");
else printf("No\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: