Nyist 2 bracket matching

Source: Internet
Author: User
Tags cmath

 

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.