#C12421. Count Words in Book Titles
Count Words in Book Titles
Count Words in Book Titles
Given a list of book titles, count the frequency of each distinct word across all titles. A word is defined as a maximal sequence of alphanumeric characters (letters and digits), underscores, and hyphens. The extraction is based on the regular expression \(\texttt{\textbackslash\b[\textbackslash\w-]+\textbackslash\b}\). Note that the matching is case-sensitive, so words with different cases are considered distinct. The output should list each word along with its count, in the order that the word first appears in the input.
Input Format: The first line contains an integer \(n\) (\(0 \le n \le 10^4\)), the number of book titles. The following \(n\) lines each contain a book title (which may include spaces, alphanumeric characters, hyphens, and underscores).
Output Format: For each distinct word, output a line with the word and its count separated by a space, in the order they first appear.
inputFormat
The first line contains an integer \(n\), the number of book titles. Each of the following \(n\) lines contains one book title.
outputFormat
Output each distinct word and its frequency on a separate line in the format:word count
with words listed in the order they first appear in the input.
4
The Great Gatsby
Great Expectations
The Catcher in the Rye
Catch-22
The 2
Great 2
Gatsby 1
Expectations 1
Catcher 1
in 1
the 1
Rye 1
Catch-22 1
</p>