|Online MIPT programming contest||РУССКИЙ|
Time limit = 5 second(s)Mathematical expressions consist of operators and operands. In turn some operators, such as '+', '-', '*' are associative, i.e. for any numbers a, b, c fairly following: a + (b + c) = (a + b) + c, and some haven't this property, for example, operators '/': (a / b) / c ≠ a / (b / c). If expression has no parantheses we should determine sequence of operator evaluation. The concept of the left and right associativity is defined. In programming languages each operator has the concrete priority and associativity.
Let we have a following set of operators:
and expression 1+2*3-4/5/6*7^9^10+(7*8-1). To define sequence of evaluation we can just insert parantheses. Lets see abstract syntax tree:
The answer is unique: (((1+(2*3))-(((4/5)/6)*(7^(9^10))))+((7*8)-1))
Input. Some first lines describe operators. Operators listed in one line have same priority. Line number correspons to operator's priority. Each line begins with type of associativity (LEFT or RIGHT) operators containing in it, then (through a blank) operators are listed. After the description of operators one line setting the alphabet from which consist operands can follows. Last line contains expression in which it is necessary to place brackets. Input expression may contain brackets. Not all of them may present in output.
Output. One line containing expression, with the brackets placed in it. Number of brackets pairs should be equal to number of binary operators.
LEFT + - LEFT * / RIGHT ^ ALPHABET 0123456789 1+((2))*3-4/(5)/6*7^9^10+(7*8-1)
RIGHT = RIGHT * / ALPHABET abcdefghijklmnop a=b=c=d*e*f*g*h/i/j/k/l
LEFT , RIGHT = += *= LEFT + - LEFT * / RIGHT ^ ALPHABET 0123456789abcdefghijklmnop abc=f,a=b+c+d/1/2/3,e+=f+=g+=add*4*5+6*7*8-9-10
Classic problem. Tests and descripton -- Tatiana Zinov'eva.
7 December 2006
© acm.mipt DevGroup
The page was generated in 240ms