Bracket pairing problem time limit: theMs | Memory Limit:65535KB Difficulty:3
-
-
Describe
-
Now, with a sequence of parentheses, you should check that the line brackets are paired.
-
-
Input
-
-
the first line enters a number n (0<n<=100), which indicates that there are n sets of test data. The next n lines enter multiple sets of input data, each set of input data is a string s (S is less than 10000, and S is not an empty string), and the number of test data groups is less than 5 groups. Data guarantee S contains only "[", "]", "(", ")" Four characters
-
-
Output
-
The
-
output of each set of input data is one row, if the parentheses contained in the string are paired, the output is yes, and if you do not pair the output no
-
-
Sample input
-
-
3[(]) (]) ([[[] ()])
-
-
Sample output
-
-
Nonoyes
-
-
Source
-
-
Network
-
Uploaded by
Naonao
Analysis: This problem examines the use of stacks, in addition to note that the two equations && connection sometimes can not exchange positions
#include <iostream> #include <stdio.h> #include <string.h> #include <stack>using namespace std; Char S[12000];int main () {int n; Stack<char> Q; cin>>n; while (n--) {cin>>s; int Len=strlen (s); if (s[0]== ') ' | | s[0]== ') {printf ("no\n"); Continue } while (!q.empty ()) Q.pop (); int flag=1; for (int i=0;i<len;i++) {if (s[i]== ' | | | s[i]== ' [') Q.push (S[i]); if (s[i]== ') ') {if (!q.empty () && q.top () = = ' (') q.pop (); else {flag=0; printf ("no\n"); Break }} if (s[i]== '] ') {if (!q.empty () &&q.top () = = ' [')//&& here Before and after the equation cannot be exchanged, if written q.top () = = ' [' &&!q.empty () is wrong q.pop (); else {flag=0; printf ("no\n"); Break }}} if (!q.empty () &&flag==1) printf ("no\n"); if (Q.empty () &&flag==1) printf ("yes\n"); } return 0;}
Nyoj Topic 2 Bracket pairing problem