Shortening Links
Time limit2sMemory limit1024 MB
Given a length limit l and a message, shorten each http:// link from left to right into a prefix plus "..." until the message fits, then report any overflow.
- Level
Medium4 of 10
- Topics
- String, Simulation, Greedy
- Solved
- No attempts yet
Problem
Vasya decided to build his own social network. Any user can write a message to another user, but a message may contain at most characters. The character limit changes every day.
Vasya finished the site and showed it to his friend Petya. Petya suggested shortening links so that messages can carry more information. Help Vasya implement link compression.
A link is a string that starts with http://, followed by lowercase Latin letters, digits, and the characters /, :, or .. There must be at least one character after http://. A link is preceded by a space or the start of the line, and followed by a space or the end of the line.
A shortened link is any prefix of the original link (zero or more of its first characters) followed by three dots .... The prefix should be as long as possible. Links are processed from left to right. A link is shortened only as much as needed to make the message fit within characters. If the message still does not fit after a link is shortened to ..., that link stays at ... and processing moves on to the next link.
For example, http://pda.lenta.ru/news/2012/01/09/sudoku/ can be shortened to http://pda.lenta.ru/news/2012/01/0... or to ....
Input
The first line contains one integer (), the limit on the message length. The second line contains the message . The message is not empty, has at most 10000 characters, and ends with a newline that is not part of the message. The message contains at least one link.
Output
The first line contains the compressed message. The second line contains 0 if the compressed message fits within the limit. Otherwise, it contains the number of characters by which the message exceeds the limit.