Trending Topic
Time limit1sMemory limit128 MB
Maintain word counts over a rolling 7-day window and answer each top N query in frequency order with ties included.
- Level
Medium5 of 10
- Topics
- Sliding window, Hash map, Sorting
- Solved
- No attempts yet
Problem
A company that analyzes text on the web is hiring, and one of its tests asks you to write a program that keeps a set of trending topics up to date. Whether you get the job depends on how efficient your program is.
The company hands you text collected from the most active blogs, grouped one batch per day. When a query arrives, report the most frequent words over the last 7 days of text, counting the day that just ended.
Input
Each input file holds one test case. The text for a single day sits between a <text> line and a </text> line.
A query for the top words can appear between the texts of two different days, written as a tag like <top 10 />. The number is always surrounded by whitespace, so a query reads as three whitespace separated tokens: <top, the number, then />.
Input ends at end of file.
Output
Answer every query in the order it appears. For a query <top N />, print a line <top N>, then one line per reported word in the form word count, then a line </top>.
Sort words by decreasing number of appearances, and alphabetically among words that appear the same number of times. Print every word whose counter of appearances equals that of the word at position , even when this makes the list longer than words. If the last 7 days hold fewer than words of interest, print all of them.
Constraints
- Every word is made of lowercase letters only and is at most 20 characters long.
- At most 20000 different words appear.
- At most 20000 words appear per day.
- Words shorter than four characters are of no interest and are never counted.
- There are at most 1000 days.