UVA 673 Parentheses Balance

 

题意:

  匹配括号,看是否所有括号都匹配。

分析:

  空行的时候要输出yes,其它的用栈正常做就行。

代码:

  

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <stack>
using namespace std;
bool judge(char a,char b)
{
if(a=='['&&b==']')return 1;
if(a=='('&&b==')')return 1;
return 0;
}
bool left(char a)
{
if(a=='['||a=='(')return 1;
return 0;
}
int main()
{
int cas;
char s[200];
scanf("%d",&cas);
getchar();
while(cas--)
{
stack<char>q;
gets(s);//cout<<strlen(s);
if(strcmp(s,"\n")==0)
{
cout<<"YES"<<endl;
continue;
}
int i,j,k;
for(i=0;s[i];i++)
{
if(q.empty())
{
q.push(s[i]);
}
else if(!judge(q.top(),s[i]))
{
if(left(s[i]))
q.push(s[i]);
}
else q.pop();
}
if(q.empty())
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
}
posted @ 2015-10-15 12:40  幻世沉溺  阅读(155)  评论(0编辑  收藏  举报