#K59042. Hashtag Frequency Counter
Hashtag Frequency Counter
Hashtag Frequency Counter
You are given a series of social media posts. Each post is a string that may contain one or more hashtags. A hashtag is defined as a '#' character followed by 1 to 50 alphanumeric characters or underscores, i.e. it matches the regular expression \#\w{1,50}
in \(\LaTeX\) format.
Your task is to implement a program that reads these posts from standard input (stdin) and then answers queries asking for the top n most frequent hashtags. For each query, output the hashtags in descending order of frequency, separated by a single space, to standard output (stdout). If there are ties in frequency, sort the tied hashtags in lexicographical order (ascending). If a post does not contain any hashtag, it should be ignored.
Input Format: The first line contains an integer T, the number of posts. The next T lines each contain a post. The following line contains an integer Q, the number of queries. The next Q lines each contain an integer n, representing the number of top hashtags to be printed.
Output Format: For each query, output a single line containing the top n hashtags separated by a single space. If there are fewer than n hashtags available, output all the available hashtags. If no hashtags appear, output an empty line.
inputFormat
The input is given via stdin and has the following structure:
T post_1 post_2 ... post_T Q n_1 n_2 ... n_Q
Where:
T
is the number of posts.- Each
post_i
is a string representing a social media post. Q
is the number of queries.- Each
n_i
is an integer indicating the top number of hashtags to output.
outputFormat
For each of the Q
queries, output one line (to stdout) which contains the top n hashtags separated by a space. If there are fewer than n hashtags, output all available hashtags. In case no hashtags exist, output an empty line.
2
This is a #fun post with #coding and #fun
Another #fun post with #learning and #coding
1
2
#fun #coding
</p>