Matching brackets (string)

Source: Internet
Author: User

Problem description:
Check whether the parentheses in the string expression match;
The number of left parentheses is not matched if the number of parentheses is not equal;
Remove the Left or Right brackets and keep the parentheses first;
Remove invalid parentheses after matching: (expression) should be (expression );
Only parentheses are considered, not the case where parentheses appear first;
Required implementation function: (the maximum length of a string is 60. You do not need to consider whether the expression is correct)
Void bracket (char * SRC, char * DST );
If yes, the original string is output through DST;
If the parameter does not match, the number of matched strings is greater than that of the string after the brackets are output through DST.
Example
Input: 12 + (345*25-34) Output: 12 + (345*25-34)
Input: 12 + (345 * (25-34) Output: 12 + (345*25-34)
Input: (12 + 345) * 25)-34 output: (12 + 345) * 25-34
Input: (543 + (256-43) * 203) + 24
Output: (543 + (256-43) * 203) + 24
Input: (1 + 2) * (34-2) + (2*8-1)
Output: (1 + 2) * (34-2) + 2*8-1)

# Include <iostream> # include <vector> # include <string> # include <algorithm> using namespace STD; void leftright (string S, vector <int> & leftbracket, vector <int> & rightbracket) {unsigned Len = S. size (); leftbracket. clear (); rightbracket. clear (); For (unsigned I = 0; I <Len; I ++) {If (s [I] = '(') leftbracket. push_back (I); else if (s [I] = ') rightbracket. push_back (I) ;}} void bracket (char * SRC, char * DST) {vector <int> L Eftbracket, rightbracket; // subscript unsigned Len = strlen (SRC); string S (SRC, SRC + Len); leftright (S, leftbracket, rightbracket ); unsigned len1 = leftbracket. size (), len2 = rightbracket. size (); While (len1> len2) {S. erase (leftbracket [len1-1], 1); leftright (S, leftbracket, rightbracket); len1 --;} while (len1 <len2) {S. erase (rightbracket [len2-1], 1); leftright (S, leftbracket, rightbracket); len2 --;} unsigned K1, K2; vector <int> era; // subscript for the element to be deleted (k1 = len1-1; k1> 0; K1 --) // Delete the undesired bracket pair {If (leftbracket [k1]-leftbracket [k1-1]! = 1) {for (k2 = 0; K2 <len2; K2 ++) {If (leftbracket [k1] <rightbracket [k2]) {rightbracket [k2] = 0; break;} // end for} // end if else {for (k2 = 0; K2 <len2-1; K2 ++) {If (leftbracket [k1] <rightbracket [k2] & leftbracket [k1-1] <rightbracket [k2 + 1] & rightbracket [k2 + 1]-rightbracket [k2] = 1) {era. push_back (leftbracket [k1]); era. push_back (rightbracket [k2]); rightbracket [k2] = 0; break ;}// end for}/end else}/E Nd forif (era. Size ()! = 0) // Delete redundant arc {sort (era. begin (), era. end (); For (INT I = era. size ()-1; I> = 0; I --) {S. erase (ERA [I], 1) ;}} strncpy (DST, & S [0], S. size (); DST [S. size ()] = '\ 0';} void main () {char * src = "(1 + 2) * (34-2 )) + (2*8-1) "; char * DST = new char [100]; bracket (SRC, DST); puts (DST );}

Note:

If the erase function of string is used, the correspondence between the size of string and the previous subscript and the string is changed. Pay special attention to this programming!

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.