#C13725. Keyword Search in Files

    ID: 43295 Type: Default 1000ms 256MiB

Keyword Search in Files

Keyword Search in Files

You are given N files, each with a file name and content. The files may be in one of three formats: .txt, .json, or .csv. You are also given a keyword. Your task is to search for the given keyword in each file following the rules below and then output two things:

  • A list of file names (in the order of input) that contain at least one occurrence of the keyword.
  • The total number of occurrences of the keyword across all files.

Rules for searching in a file:

  • .txt: Treat the entire content as plain text and count the occurrences of the keyword.
  • .json: Parse the content as JSON and then convert it back to a string (as if by using json.dumps in Python). Count the occurrences of the keyword in that string.
  • .csv: Treat each line as a CSV record. Split the record by commas, join the fields with a single space, and then count the occurrences of the keyword in the resulting string.

The search is case-sensitive. Note that when counting occurrences, they should not overlap.

For example, if there are N files and the keyword appears a total of T times, output the list of files that contain the keyword and then output T.

inputFormat

The input is given via standard input in the following format:

N
filename_1
L1
line_1
line_2
... (L1 lines total)
filename_2
L2
line_1
line_2
... (L2 lines total)
...
filename_N
LN
line_1
line_2
... (LN lines total)
keyword

Here, N is the number of files. For each file, the first line is the file name (which ends with .txt, .json, or .csv), the next line is an integer L representing the number of lines in the file, followed by L lines of file content. Finally, the last line contains the keyword to be searched for.

outputFormat

The output should be printed to standard output in the following format:

file_name_1 file_name_2 ... file_name_k
T

The first line should list the names (separated by a single space) of all files that contain at least one occurrence of the keyword. If no file contains the keyword, output an empty line. The second line should contain an integer T representing the total number of occurrences of the keyword across all files.

## sample
3
file1.txt
1
hello world
file2.json
1
{"greeting": "hello there"}
file3.csv
2
no,hello
world, test
hello
file1.txt file2.json file3.csv

3

</p>