TheSum
SRM 437 · 2009-03-24 · by Vasyl[alphacom]
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.
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.
44 77 11 1 1 1 Returns: 1
We just need to insert the digit 2 between the two 1's.
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.
100 100 10000 1000000 1000000 0 Returns: 1000000
Every possible way requires at least one insert or delete operation.
23456765 19876 927547301 7744 9876 9977 Returns: 48697
5 69 35 84 44 63 Returns: 126
Submissions are judged against all 79 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class TheSum with a public method int minCost(int a, int b, int c, int insCost, int delCost, int repCost) · 79 test cases · 2 s / 256 MB per case