This page is still under construction.

Parts of this page are still being built. What you see may change.

Document Indexing

Time limit2sMemory limit128 MB

Summary
Paginate a document by line-count and paragraph rules, then print each word in uppercase with its pages, collapsing runs of three or more consecutive pages into ranges.
Level

Medium6 of 10

Topics
Simulation, Implementation, Hash map, Sorting
Solved
No attempts yet

Problem

You are building a text editor for a text-mode operating system, and its hardest component is document indexing. An index of a document is the lexicographically ordered list of every word that occurs in the document, each followed by the page numbers on which it appears.

A document is a sequence of paragraphs. Each paragraph is one or more lines, and consecutive paragraphs are separated by exactly one blank line.

The document is first paginated — split into pages. Each page holds up to nn lines. Lines are placed on the current page one after another until nn lines have been placed, and then the following correction rules are applied:

  • If the last line on a page is the last line of its paragraph, the blank line that follows it is skipped and is placed on no page. As a result, a page never begins with a blank line.
  • If the last line on a page is the first line of a paragraph that has more than one line (an orphan line), that line is moved to the next page.
  • If the last line on a page is the next-to-last line of a paragraph that has more than three lines, that line is moved to the next page; otherwise the paragraph's last line would be left alone on a page (a widow line).
  • If the last line on a page is the next-to-last line of a paragraph that has exactly two or three lines, the whole paragraph is moved to the next page, so that neither an orphan nor a widow line remains.

After the corrections are applied, the next page is formed, and so on until the whole document has been paginated.

A word is a maximal run of English letters; case is ignored. For every word the index lists, in ascending order and separated by commas, the pages on which it occurs. When a word occurs on three or more consecutive pages, that range is written as its first and last page numbers joined by a dash, for example 3-5,7-10,12,13,15.

Input

The first line contains the integer nn (4≤n≤1004 \le n \le 100). The remaining lines are the document to be indexed; the total input size does not exceed 20,000 bytes.

A line is blank only if it is completely empty. No line has leading or trailing spaces, the document never contains two consecutive blank lines, its first line is not blank, and every line is at most 200 characters long.

Output

Print every word that occurs in the document, one per line, in lexicographical order. After each word print a single space followed by the list of pages on which it occurs, formatted as described above. Print all words in uppercase letters.

Examples1

  1. Example 1

    Input
    6
    From thousands of teams competing in regional 
    contests held from September to December 2004 
    world-wide, seventy-five teams will advance to 
    the World Finals in Shanghai, April 3-7, 2005.  
    
    Awards, prizes, scholarships, and bragging rights 
    will be at stake for some of the world's finest 
    university students of the computing science.
    
    Join us for the challenge, camaraderie, 
    and the fun! Become the best of the best
    of the best in ACM ICPC!
    
    ACM ICPC is the best contest!
    
    Expected output
    ACM 3
    ADVANCE 1
    AND 2,3
    APRIL 1
    AT 2
    AWARDS 2
    BE 2
    BECOME 3
    BEST 3
    BRAGGING 2
    CAMARADERIE 3
    CHALLENGE 3
    COMPETING 1
    COMPUTING 2
    CONTEST 3
    CONTESTS 1
    DECEMBER 1
    FINALS 1
    FINEST 2
    FIVE 1
    FOR 2,3
    FROM 1
    FUN 3
    HELD 1
    ICPC 3
    IN 1,3
    IS 3
    JOIN 3
    OF 1-3
    PRIZES 2
    REGIONAL 1
    RIGHTS 2
    S 2
    SCHOLARSHIPS 2
    SCIENCE 2
    SEPTEMBER 1
    SEVENTY 1
    SHANGHAI 1
    SOME 2
    STAKE 2
    STUDENTS 2
    TEAMS 1
    THE 1-3
    THOUSANDS 1
    TO 1
    UNIVERSITY 2
    US 3
    WIDE 1
    WILL 1,2
    WORLD 1,2