Blue Bridge Cup algorithm training ALGO-57 remove extra brackets

Source: Internet
Author: User

Algorithm training Remove extra brackets time limit: 1.0s memory Limit: 512.0MB problem description Enter a arithmetic expression with parentheses from the keyboard, requiring the removal of the extra parentheses that may be included, and the result is to keep the relative position of the variables and operators in the original expression unchanged and equivalent to the original expression, without requiring simplification. In addition, ' + '-' is not considered as a case of a sign, that is, the input expression does not appear (+a) or (-a). Enter a format expression string that is not more than 255 in length and contains no space characters. All variables in the expression are a single lowercase English letter, and the operator has only the Plus + minus-multiply * divide/wait operation symbol. Expression sample input after removing extra brackets in output format
Example one: A + (B+C)-D sample two: a+b/(c+d) sample three: (A*B) +C/D sample four: ((a+b) *f)-(i/j)

Sample Output Example one:

A+b+c-d Sample two: a+b/(c+d) sample three: A*B+C/D Sample IV: (A+B) *f-i/j

Topic Analysis:

Iterate through the input expression, and if you find an opening parenthesis, find the closing parenthesis that corresponds to it, and whether the parentheses can be deleted based on the parentheses around and the middle content. In the process of recursive processing, if the parentheses are found, the first processing of the found parentheses can be deleted, after processing, return the last parenthesis, continue the recursive processing, until all the parentheses processing is complete.

There are several situations in which parentheses are not allowed to be deleted:

    1. The brackets are "-", and the brackets are "+" or "-" and cannot be deleted;
    2. "/" in front of brackets, cannot be deleted;
    3. The brackets are "*", and the brackets are "+" or "-" and cannot be deleted;

There are several situations where you can delete:

    1. Before and after the brackets are "+" or "-", the brackets are "+" or "-" or "*" or "/" can be deleted, but note: if the brackets are "-", the brackets are "+" or "-", in the front has been processed, so you can exclude this situation;
    2. "*" before parentheses, "*" or "/" in parentheses, "+" or "-" or "*" or "/" after parentheses, can be deleted;

Other conditions cannot be deleted.

Example code:

1#include <stdio.h>2 3 //detect if parentheses can be deleted4 intCheckCharS[],intLeftintRight )5 {6     intI// Subscript7     intLeftcount;//left parenthesis Statistics8     9     //processing '-(a +|-b) 'Ten     if(s[left-1] =='-') One     { Ai =Left ; -Leftcount =1; -          while(++i <Right ) { the             if(S[i] = ='(') -             { -leftcount++; -             } +             Else if((s[i] = ='+'|| S[i] = ='-') && Leftcount = =1) -             { +                 return 0; A             } at         } -     } -      -     //processing '/(a +|-|*|/b) ' -     if(s[left-1] =='/') -     { in         return 0; -     } to      +     //processing ' + (a +|-|*|/b) +|-' -     if(s[left-1] !='*'&& s[left-1] !='/'&& thes[right+1] !='*'&& s[right+1] !='/') *     { $         return 1;Panax Notoginseng     } -      the     //Processing ' * (a *|/B) +|-|*|/' +i =Left ; ALeftcount =1; the      while(++i <Right ) { +         if(S[i] = ='(') -         { $leftcount++; $         } -         Else if((s[i] = ='*'|| S[i] = ='/') && Leftcount = =1) -         { the             return 1; -         }Wuyi     } the     return 0; - } Wu  - //remove the extra parentheses About intDelexcessbrackets (CharS[],intindex) $ { -     intLeft , right; -      while(S[index]! =' /') { -         if(S[index] = =')')//if it is a closing parenthesis, return the subscript A         { +             returnindex;  the         } -         if(S[index] = ='(')//If an opening parenthesis is left, the subscript of the closing parenthesis is found $         { theleft =index; theindex = right = Delexcessbrackets (s, index+1); the              the             if(check (s, left, right))//if the test result can be deleted, then replace the parenthesis position with an empty -             { inS[left] = S[right] =' ';  the             } the         } Aboutindex++; the     }         the }  the  + intMain () - { the     Charexp[ the];Bayiscanf"%s", exp); the      theDelexcessbrackets (exp,0); -      -     inti =-1; the      while(Exp[++i]! =' /') { the         if(Exp[i]! =' ') the         { theprintf"%c", Exp[i]); -         } the     } the      the     return 0;94}

Blue Bridge Cup algorithm training ALGO-57 remove extra 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.