Data structure Experiment stack four: brace matching Time limit:1000ms Memory limit:65536k The title describes to you a string of characters, not exceeding 50 characters, which may include parentheses, numbers, letters, punctuation marks, spaces, and your task is to check () in this string of characters, [],{} matches.
Input input data has multiple groups, processing to the end of the file.
Output "yes" if matched, mismatch output "no"
Sample input
Sin (20+10) {[}]
Sample output
Yesno
Note: The strings you read may contain spaces.
#include <iostream> #include <string> #include <stdio.h> #include <string.h> #include <map > #include <stack> #include <deque>//double-ended queue # include <queue> #include <algorithm> #include < Ctype.h>using namespace Std;int Main () {char s[100]; int I, J; while (gets (s)!=null) {unsigned int len=strlen (s); stack<char>q; for (i=0; i<len; i++) {if (s[i]== ' | | | s[i]== ' [' | | | s[i]== ' {') {Q.push (s[i]); } else if (s[i]== ') ') {if (Q.empty () | | q.top ()! = ' (') { Q.push (S[i]); } else if (q.top () = = ' (') {q.pop (); }} else if (s[i]== '] ') {if (Q.empty () | | | q.top () = ' [') { Q.push (S[i]); } else if (q.top () = = ' [') {Q.pop (); }} else if (s[i]== '} ') {if (Q.empty () | | | q.top ()! = ' {') { Q.push (S[i]); } else if (q.top () = = ' {') {q.pop (); }}} if (Q.empty ()) printf ("yes\n"); else printf ("no\n"); } return 0;}
STL version Brackets Match (not feel as fast as you used to emulate the array)