突然发现有点问题uva673(栈括号匹…

来源:互联网 发布:淘宝奢侈品代购 编辑:程序博客网 时间:2024/04/29 20:33

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=8&problem=614&mosmsg=Submission+received+with+ID+10357983

673 - Parentheses Balance

Time limit: 3.000 seconds

Parentheses Balance

You are given a string consisting of parentheses () and[]. A string of this type is said to becorrect:

(a)
if it is the empty string
(b)
if A and B are correct, AB is correct,
(c)
if A is correct, (A) and[A] is correct.

Write a program that takes a sequence of strings of this typeand check their correctness. Your program can assume that themaximum string length is 128.

Input

The file contains a positive integer n and a sequence ofn strings of parentheses () and [], onestring a line.

Output

A sequence of Yes or No on the outputfile.

SampleInput

3([])(([()])))([()[]()])()

SampleOutput

YesNoYes



Miguel Revilla
2000-08-14题意:括号匹配; #include<stdio.h>
#include<string.h>
int n;
char q[130],str[130];
int top,len;
int main()
{
 scanf("%d",&n);
 getchar();
 int i,j;
 for(i=0;i<n;i++)
 {
  gets(str);
  len=strlen(str);
  top=0;
  if(len==0)
  {
   printf("Yes\n");
   continue;
  }
  q[0]=str[0];//关键
  for(j=1;j<len;j++)
  {
   if(str[j]=='('||str[j]=='[')//'('和'['入栈
   {
    q[++top]=str[j];//指针先加一再入栈
   }
   elseif(str[j]==')')
   {
    if(q[top]=='(')
     top--;
    else
     q[++top]=str[j];
   }
   elseif(str[j]==']')
   {
    if(q[top]=='[')
     top--;
    else
     q[++top]=str[j];
   }
  }
  if(top==-1)
   printf("Yes\n");
  else
   printf("No\n");
 }
 return 0;
}