Stickers
Time limit0.5sMemory limit256 MB
Parse a comma-separated list of sticker numbers and ranges with leading zeros, deduplicate, then output the shortest valid representation with fewest commas.
- Level
Medium7 of 10
- Topics
- String, Implementation, Sorting, Greedy
- Solved
- No attempts yet
Problem
Mario collects stickers for an Animal Kingdom album.
Every evening he goes through his album and writes down the numbers of the stickers he is missing, separated by commas ,. Sometimes, instead of writing out a sequence of consecutive missing stickers, he writes only the first and the last number of that sequence, joined by a minus sign -.
For example, if Mario writes 1,3,4,5,115-120 on paper, he is missing the stickers numbered 1, 3, 4, 5, 115, 116, 117, 118, 119, and 120.
Mario is very clumsy and the album holds a very large number of stickers, so he often writes the same sticker more than once or accidentally adds leading zeros to some numbers.
Write a program that reads Mario's record and finds a string with the minimum number of characters that represents the stickers Mario is missing.
Input
The first line contains a string, Mario's record. The string has at most 500000 characters, and the only characters in it are decimal digits, commas, and minus signs.
The record is valid, that is, it follows the format described in the statement.
Note: sticker numbers can be very large.
Output
Print, on the first and only line, the shortest possible string that represents the stickers Mario is missing. If several shortest strings exist, choose the one with the fewest commas ,.
If several strings still qualify, print the one that follows these rules:
- List the comma-separated items in ascending order of sticker number.
- Write each range as
a-bwith the smaller numberafirst.