uva 673 Parentheses Balance

水题照题意模拟即可

 

#include <stdio.h>

#include <string.h>

#define MAX 130

char s[MAX];



int main()

{

    int i,n,top,len;  char temp[MAX],ch;  

    scanf("%d",&n); getchar();

    while(n--)

    {

        gets(temp); len=strlen(temp); if(len==0)  { printf("Yes\n"); continue; }

        for(top=0,i=0; i<len; i++)

        {

            if(temp[i]!='(' && temp[i]!=')' && temp[i]!='[' && temp[i]!=']') continue;

            s[top]=temp[i]; if(top==0)  {top++; continue;}

            if( (s[top-1]=='(' && s[top]==')' ) || (s[top-1]=='[' && s[top]==']') ) top--;

            else top++;

        }

        if(top<=0) printf("Yes\n");  else printf("No\n");

    }

    return 0;

}

你可能感兴趣的:(uva)