#C4677. Common Words in Sentences
Common Words in Sentences
Common Words in Sentences
You are given t sentences. Your task is to find all words that appear in every sentence. The words in the output should be in the same order as they first appear in the first sentence.
If there is no common word among all sentences, output an empty line.
Note: A word is defined as a contiguous sequence of non‐space characters. Make sure to check for exact matches.
For example, given the sentences:
- "hello world"
- "hello there"
- "hello my friend"
The common word is: hello.
The problem can be modeled mathematically as follows. Let \( S_1, S_2, \dots, S_t \) be the sets of words in each sentence. Then, the output is the list of words in \( S_1 \) (preserving the order in which they appear) that are contained in the intersection \( \bigcap_{i=1}^{t} S_i \).
inputFormat
The input is read from standard input (stdin). The first line contains an integer t
indicating the number of sentences. The next t
lines each contain a sentence.
For example:
3 hello world hello there hello my friend
outputFormat
Print the common words separated by a single space in the order they appear in the first sentence. If there are no common words, print an empty line.
For example, for the sample input above, the output should be:
hello## sample
3
hello world
hello there
hello my friend
hello
</p>