TopCoder problem "RestoreExpression" used in SRM 323 (Division I Level Three)



Problem Statement

    Consider a simple arithmetic expression, A+B=C, where A, B, C are nonnegative integers without leading zeroes. Unfortunately, some digits are unknown and are replaced by the symbol '?'. You task is to restore the expression.

You are given a String expression. The resulting String must contain the same expression, but with the symbols '?' replaced by digits so that the expression is correct. Numbers in the resulting String must not have leading zeroes.

For example, given the string "5+?=?4" you should return "5+9=14".

If multiple solutions exist, return the one with the maximum possible C. If more than one such solution exists, return the one among them that maximizes the value of A. If there is no solution, return "no solution" (quotes for clarity).
 

Definition

    
Class:RestoreExpression
Method:restore
Parameters:String
Returns:String
Method signature:String restore(String expression)
(be sure your method is public)
    
 

Constraints

-expression will contain between 5 and 50 characters, inclusive.
-expression will be formatted as "A+B=C" (quotes for clarity), where A, B and C are nonnegative integers (without leading zeroes) with some digits possibly replaced by '?'.
 

Examples

0)
    
"5+?=?4"
Returns: "5+9=14"
This is the example from the problem statement. 5+9=14 is the only solution.
1)
    
"?+?=4"
Returns: "4+0=4"
There are five possible solutions, all of which have the same value of C. 4+0=4 maximizes A.
2)
    
"?2+?2=4"
Returns: "no solution"
3)
    
"??+1=1?"
Returns: "18+1=19"
4)
    
"???+?=???0"
Returns: "999+1=1000"

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=6580

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=10003&pm=6580

Writer:

Pawa

Testers:

PabloGilberto , brett1479 , Olexiy , lovro

Problem categories:

Brute Force