Making the Best Phone Number
Time limit2sMemory limit128 MB
Split a digit string into groups of 2 or 3 to maximize a score based on group types, then output the lexicographically smallest optimal formatting.
- Level
Medium6 of 10
- Topics
- Dynamic programming, String, Greedy, Implementation
- Solved
- No attempts yet
Problem
Youngsik wants to share his new mobile phone number with friends in a form that is easy to remember. The phone number is a string of N digits, and it must be split with hyphens (-). Each group must contain exactly 2 or 3 digits.
Each group belongs to one of the following three types.
- O group: all digits in the group are the same. For instance,
000and77. - G group: the group has length 3, and exactly two of its digits are the same. For instance,
030,229, and116. - Z group: all digits in the group are different. For instance,
123and90.
The quality of a formatted phone number is defined as follows.
2 * (number of O groups) + (number of G groups)
Given Youngsik's phone number, output a valid formatting whose quality is as large as possible.
Input
The first line contains Youngsik's phone number.
The phone number consists only of digits from 0 to 9, and its length is between 2 and 1,000 inclusive.
Output
Output a valid formatting of the phone number with maximum quality.
Separate adjacent groups with hyphens (-). If there is more than one maximum-quality formatting, output the lexicographically smallest one. String comparison uses the following order.
- < 0 < 1 < 2 < 3 < 4 < 5 < 6 < 7 < 8 < 9
Hint
For the first visible test, the phone number can be split in several ways.
508-86-38: quality 050-886-38: quality 150-88-638: quality 2
Therefore the maximum-quality formatting is 50-88-638.