Secret Code: Largest Number
Time limit3sMemory limit128 MB
Given a noisy string, find the largest decimal number it could decode to, either fixing one language for all digits or allowing a different language per digit, using digit-word subsequence matching.
- Level
Hard8 of 10
- Topics
- Dynamic programming, String matching, Greedy, String
- Solved
- No attempts yet
Problem
Agent Smith transmits secret information to a secret center: a single strictly positive integer. To encode the number he replaces each digit with the corresponding word and concatenates the words. For example, 42 may become FOURTWO.
Smith knows four languages (numbered 1 to 4). In these languages the digits 0 through 9 are spelled as follows (all letters are capital Latin, ASCII codes 65-90):
After encoding, he adds noise: redundant capital Latin letters. Any letters (including letters already used in the code) may be inserted in any quantity at any positions.
Write a program that reads such a code and finds the largest number it could encode, under two separate assumptions:
- The language is unknown, but the same single language, one of the four listed above, was used for the whole number.
- Smith may have encoded different digits with different languages, each one of the four listed above, or with the same one.
Input
The input contains several lines; process each line independently. Each line contains at most capital Latin letters -- one code. The total input size is less than 1 MB.
Output
For each input line, output two integers separated by a single space -- the largest numbers under the two assumptions above, in that order.
Each code is guaranteed to come from a correct positive (non-zero) integer, so both answers are always positive. Print them without leading zeroes.