673
Parentheses Balance
题解:栈的基本使用,左括号入栈,右括号就判断是不是和栈首匹配,注意空串和每次栈的清空。
AC代码:
#include<cstdio>
#include<stack>
#include<cstring>
#include<bits/stdc++.h>
using namespace std;
stack<char> S;
int t;
char s[260];
bool check(char a,char b)
{
if(a=='(' && b==')')return true;
if(a=='[' && b==']')return true;
return false;
}
int main()
{
scanf("%d",&t);
getchar();
while(t--)
{
gets(s);
while(!S.empty())S.pop();
int n = strlen(s);
int ok = 1;
for(int i=0;i<n;i++)
{
if(s[i]=='('||s[i]=='[')S.push(s[i]);
else
{
if(S.empty())ok = 0;
else
{
char now = S.top();
if(check(now,s[i]))S.pop();
else ok = 0;
}
}
}
if(ok==0||!S.empty())printf("No\n");
else printf("Yes\n");
}
return 0;
}