Parentheses Balance(括號匹配)--棧
阿新 • • 發佈:2019-02-15
/*******************************************************************************************************You are given a string consisting of parentheses () and []. A string of this type is said to be correct:
- (a)
- if it is the empty string
- (b)
- if A and B are correct, AB is correct,
- (c)
- if A is correct, (A )
Write a program that takes a sequence of strings of this type and check their correctness. Your program can assume that the maximum string length is 128.
Input
The file contains a positive integer n and a sequence of n strings of parentheses () and [], one string a line.
A sequence of Yes
3 ([]) (([()]))) ([()[]()])()
Yes No Yes*******************************************************************************************************************/
#include<stdio.h>
#include<string.h>int main()
{
char q[300],w[300];
int n,j,k,i;
scanf("%d%*c",&n);
for(i=0;i<n;i++)
{
memset(q,0,sizeof(q));
gets(q);
int len=strlen(q);
for(k=0,j=0;j<len;j++)
{
if((q[j]==')' && w[k-1]=='(') || (q[j]==']' && w[k-1]=='['))
{
k--;
}
else
{
w[k]=q[j];
k++;
}
}
if(k==0)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}