#K60962. Minimum Removals for Unique Words in Stories

    ID: 31203 Type: Default 1000ms 256MiB

Minimum Removals for Unique Words in Stories

Minimum Removals for Unique Words in Stories

You are given n stories. Each story is a string consisting of words separated by spaces. Your task is to determine the minimum number of words that need to be removed such that every word remaining in the combined sequence is unique.

More formally, let the total set of words (after splitting each story by whitespace) be \(W\). For each word that appears \(c\geq 1\) times in \(W\), we need to remove \(c-1\) occurrences so that each word appears only once. The answer is the sum over all words with more than one occurrence, i.e.,

[ \text{removals} = \sum_{w \in W, ; count(w) > 1} (\text{count}(w) - 1). ]

It is guaranteed that the input format is valid.

inputFormat

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

  • The first line contains an integer n representing the number of stories.
  • The following n lines each contain a story (a sequence of words separated by spaces).

outputFormat

Output a single integer to stdout representing the minimum number of words that must be removed so that every remaining word in all stories is unique.

## sample
3
hello world
hello instabook
world wide web
2