Bracket 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
Classic Stack problem:
1 2#include <iostream>3 using namespacestd;4#include <stack>5#include <string>6 intMain ()7 {8 intN//n Group of data9Cin>>N;Tenstack<Char> SS;strings; One while(n--) A { -Cin>>s; - intlen=s.size (); the while(!ss.empty ()) - Ss.pop (); -Ss.push ('#'); - for(intI=0; i<len;i++) + { - if(s[i]=='['|| s[i]=='(') + Ss.push (S[i]); A Else at { - if(s[i]==']') - if(Ss.top () = ='[') - Ss.pop (); - Else - Ss.push (S[i]); in if(s[i]==')') - if(Ss.top () = ='(') to Ss.pop (); + Else - Ss.push (S[i]); the } * $ }Panax Notoginseng if(Ss.top () = ='#') -cout<<"yes\n"; the Else +cout<<"no\n"; A } the return 0; + } -
Nyoj 2 Bracket pairing problem