Column Addition
Time limit2sMemory limit512 MB
Given three n-digit strings, erase the fewest digit columns so the first number plus the second equals the third.
- Level
Medium7 of 10
- Topics
- Dynamic programming, String, Math, Implementation
- Solved
- No attempts yet
Problem
A column addition adds two integers written one above the other with their digits aligned.
123
+ 456
-------
579
A column addition is written on the blackboard, but the sum it shows is not necessarily correct. You may erase any number of columns so that the addition becomes correct. In the addition below, erasing the second column and the fourth column leaves a correct addition.
12127 117
+ 45618 ⇒ + 468
--------- -------
51825 585
Read the kept columns from left to right to form three numbers. The addition is correct when the first number plus the second number equals the third number. Erasing every column leaves three empty numbers, and that counts as correct.
Find the minimum number of columns you have to erase so that the remaining formula is a correct addition.
Input
The input holds several test cases. Each test case starts with a line holding a single integer n, the number of digit columns (). Each of the next three lines holds a string of n digits. The number on the third line is written as the sum of the numbers on the first two lines, but it is not necessarily correct. The input ends with a line holding a single 0, which you do not process.
Output
For each test case, print the minimum number of columns that have to be erased on its own line.