Sicily 1543. Completing Brackets

Source: Internet
Author: User
#include "iostream" #include "string" #include "stack" using namespace std;/* topic Link: http://soj.sysu.edu.cn/show_ Problem.php?pid=1543&cid= main idea: First use the stack to eliminate those that have been matched, and then see how many left and right brackets in the remaining ones, each with a closing parenthesis, with an opening parenthesis on the left side of the string, and each with an opening parenthesis, Just add a right parenthesis to the right of the string */int Main () {string S;while (cin >> s) {int flag = 0;stack<char> S1;for (size_t i = 0; i < S.S Ize (); i++) {if (!s1.empty () && s1.top () = = ' [' && s[i] = = '] ') s1.pop (); Elses1.push (S[i]);} while (!s1.empty ()) {if (s1.top () = = ' [') {s + = '] '; S1.pop ();} Else{s = ' [' + S;s1.pop ();}} cout << s << endl;} return 0;}

Sicily 1543. Completing Brackets

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.