← 返回 pinterest 的题目列表Minimize Result by Adding Parentheses to Expression
类型:online_judge
You are given a string expression representing an expression made of decimal digits and exactly one plus sign +, e.g. "247+38".
Insert one pair of parentheses () into the expression such that the parentheses must include the + and also include at least one digit on each side of the +.
After insertion:
The content inside parentheses is evaluated as addition.
Any digits left outside the parentheses are multiplied with the parenthesized result (equivalent to inserting * between the outside digits and the parenthesized value).
Return the resulting expression string (with parentheses) that yields the minimum possible value.
Note: The original post mentions there is no digit 0 in the string.
I/O
Input: one line string expression
Output: one line string of the minimized expression with parentheses
Constraints (OA-friendly)
3 <= len(expression) <= 50
expression contains only '1'~'9' and '+'
exactly one '+'
Examples
Input:
247+38
Output:
2(47+38)
Input:
12+34
Output:
(12+3)4
Example
Input
247+38
Output
2(47+38)