"Base Exercise" "Stack" "map" codevs3543 brace matching

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.