Under construction

Still being built. Things may change

Abbreviation

Interview

Time limit1sMemory limit512 MB

Summary
Scan a text and replace each maximal run of two or more single-capitalized words separated by single spaces with their initials followed by the original run in parentheses.
Level

Medium5 of 10

Topics
String, Simulation, Implementation, Greedy
Solved
No attempts yet

Problem

An abbreviation is a shortened form of a word or a phrase. The English word comes from the Latin brevis, meaning short. Write a tool that replaces every run of capitalized words with an abbreviation built from their first upper case letters, followed by the original run in parentheses.

Here are the exact definitions. A word in the text is a maximally long run of upper and lower case English letters. A capitalized word is a word made of one upper case letter followed by one or more lower case letters. For example, "Ab", "Abc", "Abcd" and "Abcde" are capitalized words, while "ab", "A", "AB", "ABc" and "AbC" are not.

An abbreviatable run is a run of two or more capitalized words in which every two neighbouring words are separated by exactly one space. No line break and no punctuation may appear inside it.

The abbreviation of an abbreviatable run is the first (upper case) letter of each of its words in order, then one space, an opening parenthesis, the original run, and a closing parenthesis.

Every run you replace is maximal: it cannot be extended by one more capitalized word on the left or on the right.

Input

The input has at most 1000 lines and each line is at most 120 characters long. Each line consists of spaces, upper and lower case English letters, commas and dots. No line has a leading or a trailing space, no line is empty, and the input has at least one line.

Output

Print the original text with every maximal abbreviatable run replaced by its abbreviation. Keep all remaining characters, spaces and punctuation included, exactly as they appear in the input.

Examples3

  1. Example 1

    Input
    This is ACM North Eastern European Regional Contest,
    sponsored by International Business Machines.
    The. Best. Contest. Ever.
    A Great Opportunity for all contestants.
    
    Expected output
    This is ACM NEERC (North Eastern European Regional Contest),
    sponsored by IBM (International Business Machines).
    The. Best. Contest. Ever.
    A GO (Great Opportunity) for all contestants.
    
  2. Example 2

    Input
    ab Ab A Abc AB Abcd ABc Abcde AbC
    
    Expected output
    ab Ab A Abc AB Abcd ABc Abcde AbC
    
  3. Example 3

    Input
    Oh  No  Extra Spaces.And,Punctuation Ruin Everything
    
    Expected output
    Oh  No  ES (Extra Spaces).And,PRE (Punctuation Ruin Everything)