TopCoder problem "TheSum" used in SRM 437 (Division I Level Three)



Problem Statement

    

There is nothing more beautiful than just an integer number.

You are given integers a, b and c. Write each of them down in decimal notation with no leading zeros. The following operations can be performed on each of the written numbers:

  • Insert a single digit at any position of the number (possibly at the beginning or at the end).
  • Delete a single digit from the number.
  • Replace a single digit in the number with some other digit.

An operation can only be performed if it results in a positive number with at least one digit and no leading zeros.

Each operation has an associated cost - insCost, delCost and repCost, respectively. Return the minimal possible total cost of operations needed to satisfy the equality a+b=c.

 

Definition

    
Class:TheSum
Method:minCost
Parameters:int, int, int, int, int, int
Returns:int
Method signature:int minCost(int a, int b, int c, int insCost, int delCost, int repCost)
(be sure your method is public)
    
 

Constraints

-a, b, and c will each be between 1 and 1,000,000,000, inclusive.
-insCost, delCost and repCost will each be between 0 and 1,000,000, inclusive.
 

Examples

0)
    
44
77
11
1
1
1
Returns: 1
We just need to insert the digit 2 between the two 1's.
1)
    
44
77
11
4
4
1
Returns: 2
This is the same situation as above, but with different costs. This time, the insert and delete operations are much more expensive, and it is cheaper to make two replacements instead. For example, we can turn the numbers into 44+17=61 for a total cost of 2.
2)
    
100
100
10000
1000000
1000000
0
Returns: 1000000
Every possible way requires at least one insert or delete operation.
3)
    
23456765
19876
927547301
7744
9876
9977
Returns: 48697

Problem url:

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

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=13699&pm=10232

Writer:

Vasyl[alphacom]

Testers:

PabloGilberto , bmerry , ivan_metelsky

Problem categories:

Dynamic Programming