Uva673 Parentheses Balance(栈)

来源:互联网 发布:数控龙门铣编程视频 编辑:程序博客网 时间:2024/05/22 19:33

传送门:https://vjudge.net/problem/UVA-673

这是栈的一个应用:

#include <iostream>#include <cstring>#include <stack>using namespace std;const int maxn=140;char a[maxn];stack<char>sta;int main(){//freopen("data.in.txt","r",stdin);int n;scanf("%d%*c",&n);while(n--){while(!sta.empty())sta.pop();gets(a);for(int i=0;i<strlen(a);i++){if(a[i]=='('||a[i]=='[')sta.push(a[i]);elseif(a[i]==')'||a[i]==']'){if(sta.empty())sta.push(a[i]);if(a[i]==')'&&sta.top()=='(')sta.pop();if(a[i]==']'&&sta.top()=='[')sta.pop();}}if(sta.empty())cout<<"Yes"<<endl;elsecout<<"No"<<endl;}return 0;}