#C14179. Valid Word Finder
Valid Word Finder
Valid Word Finder
You are given a string s
and a dictionary of valid words. Your task is to find all the substrings of s
that match any word in the dictionary. A substring is defined as a contiguous sequence of characters in s
. Only unique valid words should be printed.
The answer should be printed in lexicographical order. If nothing from the dictionary appears in s
, output nothing.
Formally, if we denote a substring from index i to j by \(s[i:j]\), then for each dictionary word \(w\), if \(w = s[i:j]\) for some \(0 \le i < j \le |s|\), then \(w\) is considered valid. In \(\LaTeX\) you can express this condition as: \(\exists\, i,j \text{ such that } 0 \le i < j \le |s| \text{ and } s[i:j] = w\).
inputFormat
The input is read from standard input (stdin
) and has the following format:
- A single line containing the string
s
. - A single line containing an integer
n
which denotes the number of words in the dictionary. - Then follow
n
lines, each containing one word from the dictionary.
outputFormat
Output the valid words found in the string, one per line, sorted in lexicographical order. If no valid words are found, output nothing.
## sampleapplenotebook
5
apple
pen
notebook
note
book
apple
book
note
notebook
</p>