UVA 673 Parentheses Balance
栈的超级水题 直接模拟
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN=150;
char s[MAXN], stack[MAXN];
int main()
{
int t, i, j, top;
scanf("%d%*c",&t);
while(t--)
{
gets(s);
top=-1;
for(i=0; i<strlen(s); i++)
{
if(top>=0)
{
if(stack[top]=='('&&s[i]==')'||(stack[top]=='['&&s[i]==']'))
top--;
else
stack[++top]=s[i];
}
else stack[++top]=s[i];
}
if(top==-1) printf("Yes\n");
else printf("No\n");
}
return 0;
}
浙公网安备 33010602011771号