You are given a string consisting of parentheses () and []. A string of this type was said to being correct: (a) If it is the empty string (b) If A and B were correct, AB is correct, (c) If A is correct, (a) and [a] is correct.
Write a program, takes a sequence of strings of this type and check their correctness. Your program can assume, 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.
Output A sequence of Yes or No on the output file.
Sample Input
3 ([]) (([()]) ([[()]))
([() [] ()] () ()
Sample Output
Yes
No
Yes
#include <stdio.h>
#include <string.h>
#define MAX
int main () {
int n;
Char S[max];
int top;
scanf ("%d", &n);
GetChar ();
char c;
int flag;
while (n--) {
flag=0;
Top=-1;
while ((C=getchar ())!=eof&&c!= ' \ n ') {
if (top!=-1) {
if (c== '] ') {
if (s[top]== ' [')
top-- ;
else
flag=1;
}
else if (c== ') ') {
if (s[top]== ' (')
top--;
else
flag=1;
}
else{
top++;
s[top]=c;
}
}
else{
top++;
s[top]=c;
}
}
if (top!=-1) {
flag=1;
}
if (flag==1)
printf ("no\n");
else
printf ("yes\n");
}
return 0;
}
You are given a string consisting of parentheses () and []. A string of this type was said to being correct: (a) If it is the empty string (b) If A and B were correct, AB is correct, (c) If A is correct, (a) and [a] is correct.
Write a program, takes a sequence of strings of this type and check their correctness. Your program can assume, 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 Lin E.
Output A sequence of Yes or No on the output file.
Sample Input
3 ([]) (([()]) ([[()]))
([() [] ()] () ()
Sample Output
Yes
No
Yes