OperationsArrangement
SRM 285 · 2006-01-24 · by Andrew_Lazarev
Problem Statement
You are given a sequence of digits. You must insert either a '+' (addition) operator or a '*' (multiplication) operator between each pair of adjacent digits in such a way that minimizes the value of the resulting expression. The expression should be evaluated using the standard order of operations (multiplication has a higher precedence than addition).
You will be given a
Constraints
- sequence will contain between 2 and 50 characters, inclusive.
- Each character in sequence will be a digit ('0'-'9').
"222" Returns: "2*2+2"
The minimal result can be obtained in three ways: "2*2+2", "2+2*2" and "2+2+2". The first one comes first lexicographically.
"322" Returns: "3+2*2"
"307" Returns: "3*0*7"
"391118571" Returns: "3+9*1*1*1+8+5+7*1"
"111221911212" Returns: "1*1*1*2*2*1+9*1*1+2*1*2"
Submissions are judged against all 135 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class OperationsArrangement with a public method string arrange(string sequence) · 135 test cases · 2 s / 256 MB per case