#K59712. Tracking Word Frequencies in Product Reviews

    ID: 30926 Type: Default 1000ms 256MiB

Tracking Word Frequencies in Product Reviews

Tracking Word Frequencies in Product Reviews

You are given a list of products along with their customer reviews, and a list of words to track. For each product, if any review (case-insensitive) contains the substring fraud, then that product is to be ignored. Otherwise, count how many times each of the given words appears in all reviews (case-insensitive count, and a word may appear multiple times in a single review).

The input is provided via standard input. Your task is to output the product identifier and the frequencies of each word (in the order given in the input) for every product that is not ignored. Each valid product's result should be printed on a separate line. If the list of words is empty, just print the product identifier.

Note: When counting frequencies, treat both the reviews and the words to track as lowercase. Occurrences should be counted based on simple substring matching.

Mathematically, if S is a review string and w is a word to track, the frequency is computed as:

$$\text{frequency}(w, S) = \text{number of (non-overlapping) occurrences of } w \text{ in } S $$

inputFormat

The input is read from standard input and has the following format:

  • The first line contains an integer N, the number of products.
  • For each product, the following lines are given:
    • A line containing the product identifier (a string).
    • A line containing an integer R, the number of reviews for that product.
    • R lines each containing a review (a string).
  • A line containing an integer M, the number of words to track.
  • M lines each containing a word.

If N is 0, there are no products, and no further output is expected. If M is 0, then only the product identifiers of valid products should be printed.

outputFormat

For each product that does NOT have any review containing the substring fraud (case-insensitive), output a line containing the product identifier followed by the frequency counts of each given word (in the order they were provided), separated by spaces. If the list of words is empty, output only the product identifier.

Products with reviews containing "fraud" are skipped and produce no output.

## sample
3
P123
3
Excellent quality, will buy again
Not worth the price
Quality is great but expensive
P456
4
Very comfortable
Comfortable and affordable
Not comfortable at all
Highly recommend for comfort
P789
3
Poor quality
Quality is a big issue
Will not recommend
4
quality
comfortable
expensive
recommend
P123 2 0 1 0

P456 0 3 0 1 P789 2 0 0 1

</p>