UVa 673 括号平衡
时间:2014-05-11 20:19:34
收藏:0
阅读:376
思路:简单的匹配操作,利用栈。
Code:
#include<stdio.h>
#include<string.h>
char stack[135];
int main()
{
int n;
scanf("%d",&n);
getchar();
while(n-->0)
{
memset(stack,0,sizeof(stack));
char c;
int top=0;
int flag=1;
while((c=getchar())!=‘\n‘)
{
if(c==‘(‘||c==‘[‘) stack[top++]=c;
else if(c==‘)‘)
{
if(top<=0||stack[--top]!=‘(‘) flag=0;
}
else
{//printf("top:%d s[top]:%c\n",top,stack[top-1]);
if(top<=0||stack[--top]!=‘[‘) flag=0;//手误把字符[写成]了
}
}//whilec
if(flag==0||top!=0) printf("No\n");
else printf("Yes\n");
}//whilen
return 0;
}
赶紧休息,按时休息也是一种能力。。中午,晚上。
评论(0)