Problem 8: Roman Palindromes

advertisement
October 31, 2009
ACM North Central North America Regional Programming Contest
Problem 8
Problem 8: Roman Palindromes
No wonder Rome fell. Their numbering system was just messed up. Consider the number 4, which in
Roman Numerals is “IV”. If I reverse the letters and make “VI” I get 6. And in general, for any
Roman numeral letters A1 and A2, if A1’s value is < A2’s value, then A1A2 = A2 - A1. This is like “IV” = 4.
But for A2A1 you add, like “VI” = 6.
I=1
A table of the Roman Numeral symbols and their decimal equivalents is at right.
V=5
X = 10
We can generalize the A1A2 case and the A2A1 case with any of the letters. For
L = 50
example, “VC” = 95, but “CV” = 105, while “XM” = 990, and “MX” = 1010. These
C = 100
combinations can also occur “in the middle” of a Roman Numeral: “XXIX” is 2009,
D = 500
“CDXLV” is 445.
M = 1000
Now suppose one were to take a Roman numeral string and generate a palindrome.
Flip the original string end-to-end and “prepend” it in front of the original string according to an
even/odd rule: If the length of the original string is even, the first character appears twice, but if
the length is odd the first character appears only once. Thus the string “MDC” becomes “CDMDC”,
and the string “VI” becomes “IVVI”.
Apply the following rules and consider A1=string[i] and A2=string[i+1] for i=1..length-1*:
1. For letters A1 and A2, if A1’s value is < A2’s value, then A1A2 = A2-A1
2. Else if the letters A1 and A2 are A1 ≥ A2, then A1A2 = A1+A2
The result of the Roman Palindrome is the sum of the answers for all A1A2.
Examples
Input
VI
Palindrome
IVVI
MCM
MCMCM
MCMLXXIV
VIXXLMCMMCMLXXIV
Value
I and V = 5 – 1 = 4
V and V = 5 + 5 = 10
V and I = 5 + 1 = 6
Total Roman Palindrome number = 20
M and C = 1000 + 100 = 1100
C and M = 1000 – 100 = 900
M and C = 1000 + 100 = 1100
C and M = 1000 – 100 = 900
Total = 4000
V and I = 5 + 1 = 6
I and X = 10 – 1 = 9
X and X = 10 + 10 = 20
X and L = 50 – 10 = 40
L and M = 1000 – 50 = 950
M and C = 1000 + 100 = 1100
C and M = 1000 – 100 = 900
M and M = 1000 + 1000 = 2000
*
Not to be confusing, but if the arrays used to hold the strings are zero-based, this is i=0..length-2; we are
just indicating that you consider each pair of characters in the string.
October 31, 2008
ACM North Central North America Regional Programming Contest
Problem 8
October 31, 2009
ACM North Central North America Regional Programming Contest
Problem 8
M and C = 1000 + 100 = 1100
C and M = 1000 – 100 = 900
M and L = 1000 + 50 = 1050
L and X = 50 + 10 = 60
X and X = 10 + 10 = 20
X and I = 10 + 1 = 11
I and V = 5 – 1 = 4
Total = 8170
Given a string using characters from the set { I, V, X, L, C, D, M }, print the value of the Roman
Palindrome.
Input
There will be multiple input cases to consider. Each case has a single input line containing the string
of Roman numeral symbols (never more than 10) followed immediately by the end of line. The line
following the input for the last case contains only an end of line.
Output
For each input case, display the case number (1, 2, …), and the value of the Roman Palindrome. Your
output should be similar to that shown in the sample. Display a blank line after each line of output.
Sample Input
VI
MCM
MCMLXXIV
Output for the Sample Input
Case 1: total = 20
(only an end of line)
Case 3: total = 8170
October 31, 2008
Case 2: total = 4000
ACM North Central North America Regional Programming Contest
Problem 8
Download