Brace pairing problem
Time limit:MS | Memory limit:65535 KB 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
This test instructions examines the usage of the data structure-stack.
#include <iostream> #include <stack>using namespace Std;int main () { int n, m; CIN >> N; while (n--) { char str[10005]; stack<char>s;//defines a stack that stores character type data. cin >> str; for (int i = 0; Str[i]! = ' + '; i++) { if (S.empty ()) S.push (Str[i]);//If the stack is empty, the data is directly in the stack. else { if (s.top () + 1 = = Str[i] | | s.top () + 2 = = Str[i])//According to the brackets ASC code difference to determine whether to correspond to S.pop ();//If str[i] corresponds to the bracket at the top of the stack, the top Element out of the stack. else//otherwise str[i] into the stack. S.push (Str[i]); } } if (S.empty ())//After the string is processed, determine if the stack is empty. cout << "Yes" << endl;//stack null means that all parentheses correspond. else//Otherwise, the parentheses do not correspond. cout << "No" << Endl; }}
If there is a description of the error, please indicate ha!!! Together to explore progress together!!!
Nanyang OJ-2 Bracket Pairing (data structure-stack application)