#C14773. Keyword Word Count
Keyword Word Count
Keyword Word Count
You are given a set of files, each represented by several lines of text. For each file, count the occurrences of each word while preserving the order in which they first appear. Also, determine the number of times a specified keyword occurs in the file and whether the keyword is present at all.
Words are defined as contiguous sequences of non‐whitespace characters. For each file, your program should output exactly three lines:
- Line 1: The list of distinct words and their counts in the format word:count in the order of their first appearance, separated by a single space. If the file is empty, output an empty line.
- Line 2: An integer representing the total count of the keyword in the file.
- Line 3: "True" if the keyword appears at least once, otherwise "False".
For example, if a file contains the text "hello world hello
" and the keyword is "hello
", then the expected output is:
hello:2 world:1 2 True
Note: Ensure that your solution reads from standard input (stdin) and writes to standard output (stdout).
In mathematical notation, if we denote the occurrence count of each word w as \(c(w)\), and the keyword as \(k\), then the following conditions must hold:
[ \text{Output Line 1: } \bigcup_{w \in W} {w:c(w)} \quad \text{(in order)} ]
[ \text{Output Line 2: } c(k) \quad \text{and} ]
[ \text{Output Line 3: } (c(k)>0)\ ?\ \text{True : False} ]
inputFormat
The input is read from stdin and has the following format:
n L1 text_line_1 text_line_2 ... text_line_{L1} L2 text_line_1 ... text_line_{L2} ... Ln text_line_1 ... text_line_{L_n} keyword
Where:
n
is an integer representing the number of files.- For each file,
L
is an integer denoting the number of lines in that file, followed byL
lines containing the file's text. - The last line of the input is the keyword to search for (a single word).
outputFormat
For each file, output exactly three lines to stdout in the following order:
- A single line containing the word counts in the format word:count for each distinct word separated by a space. If the file is empty, output an empty line.
- A single line containing an integer representing the count of the keyword in that file.
- A single line containing either "True" if the keyword was found (i.e., count > 0) or "False" otherwise.
1
1
hello world hello
hello
hello:2 world:1
2
True
</p>