传送门: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]);
else
if(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;
else
cout<<"No"<<endl;
}
return 0;
}