Originally this is a water problem, the result because I have some careless, got one hours ... Warning
The special point of this problem is that there are four kinds of parentheses if the presence of nesting must be nested in a certain order.
Put code
codevs3543 brackets Match #include<stack> #include <cstring> #include <cstdio> #include <map>using namespace std;map<char,int> low;stack<char> s;int n;bool ok=true;char a[256];int Main () {freopen ("1.txt", " R ", stdin); Freopen (" 2.txt "," w ", stdout) low[' < ']=1;low[' (']=2;low[' [']=3;low[' {']=4;low[' > ']=5;low[') ']=6; low[']=7;low['} ']=8;//use map to deal with order problems, note that existing types cannot overload operators, and of course function + matrix (from Leo) scanf ("%d", &n); for (int i=1;i<=n;i++) { OK=TRUE;SCANF ("%s", a);//Note No &while (!s.empty ()) s.pop ();//empty stack int len=strlen (a); int j=0;while (J<len) {char ch= A[j];if ((ch== ' < ') | | (ch== ' (') | | (ch== ' [') | | (ch== ' {')) {if (S.empty ()) | | (Low[ch]<=low[s.top ()])) {//need to check if stack is empty s.push (CH);} Else{ok=false;break;}} else if ((!s.empty ()) && (Low[s.top ()]==low[ch]-4)) {S.pop ();} Else{ok=false;break;} j + +;} if (!s.empty ()) printf ("no\n"),//The stack is not empty, stating there is no match on the left half parenthesis, else if (OK) printf ("yes\n"), Else printf ("no\n");} return 0;}
--the feast of the total angle, laughs Yanyan. Swear, do not know the counter.
"Base Exercise" "Stack" "map" codevs3543 brace matching