Bracket matching problem time limit: 3000 MS | memory limit: 65535 kb difficulty: 3
-
Description
-
Now, there is a sequence of parentheses. Please check whether this line of parentheses is paired.
-
Input
-
Enter N (0 <n <= 100) in the first row, indicating that N groups of test data exist. The next n rows Input Multiple groups of input data. Each group of input data is a string of S (the length of S is less than 10000, and S is not an empty string), and the number of test data groups is less than five. Data guarantee s only contains four characters: "[", "]", "(", ")"
-
Output
-
The output of each group of input data occupies one row. If the brackets contained in the string are paired, yes is output. If the string is not paired, no is output.
-
Sample Input
-
3[(])(])([[]()])
-
Sample output
-
NoNoYes
# Include <stdio. h>
# Include <stack>
Using namespace STD;
Stack <char> S;
Bool mate (char a, char B)
{
Return A = '(' & B = ')' | A = '[' & B = ']';
}
Int main ()
{
Int T, I; char STR [2, 10010];
Scanf ("% d", & T );
Getchar ();
While (t --)
{
Gets (STR );
// Puts (STR );
While (! S. Empty () S. Pop ();
For (I = 0; STR [I]! = '\ 0'; I ++)
{
If (S. Empty () |! Mate (S. Top (), STR [I])
S. Push (STR [I]);
Else S. Pop ();
}
If (S. Empty ())
Printf ("Yes \ n ");
Else printf ("NO \ n ");
}
Return 0;
}
# Include <iostream>
Using namespace STD;
# Include <stack>
# Include <cstring>
# Include <cmath>
Int main ()
{
Stack <char> S;
Char A [100];
Int I, N;
Scanf ("% d \ n", & N );
While (n --)
{
Gets ();
{
Int K = 1;
For (I = 0; I <strlen (a); I ++)
{
If (k = 0) break;
If (A [I] = '(' | A [I] = '[') S. Push (A [I]);
If (A [I] = ']' | A [I] = ')')
{
If (S. Empty ())
K = 0;
Else
{If (ABS (S. top ()-A [I]) = 1 | ABS (S. top ()-A [I]) = 2) s. pop ();
Else K = 0;
}
}
}
If (k = 1 & S. Empty () cout <"yes" <Endl;
Else cout <"no" <Endl;
While (! S. Empty () S. Pop ();
}
}
Return 0;
}
# Include <iostream>
Using namespace STD;
# Include <stack>
# Include <cstring>
# Include <cmath>
Int main ()
{
Stack <char> S;
Char A [100];
Int I;
While (gets ())
{
Int K = 1;
For (I = 0; I <strlen (a); I ++)
{
If (k = 0) break;
If (A [I] = '(' | A [I] = '[') S. Push (A [I]);
If (A [I] = ']' | A [I] = ')')
{
If (S. Empty ())
K = 0;
Else
{If (ABS (S. top ()-A [I]) = 1 | ABS (S. top ()-A [I]) = 2) s. pop ();
Else K = 0;
}
}
}
If (k = 1 & S. Empty () cout <"yes" <Endl;
Else cout <"no" <Endl;
While (! S. Empty () S. Pop ();
}
Return 0;
}