An archivist reads a scroll written as one unbroken string of lowercase glyphs, with no spaces. A dictionary of allowed words is provided. A reading of the scroll is an ordered sequence of dictionary words that concatenate to exactly the scroll string; a word may be reused any number of times.
Count the number of distinct readings of the scroll.
Line 1: the scroll string (lowercase letters, no spaces).
Line 2: an integer m, the dictionary size.
Next m lines: one dictionary word each (lowercase letters, all distinct).
A single integer: the number of distinct readings.
Example 1
Input
aab 3 a aa b
Expected
2
Explanation
The readings are [a,a,b] and [aa,b], so the answer is 2.
Example 2
Input
aaa 2 a aa
Expected
3
Explanation
The readings are [a,a,a], [a,aa], and [aa,a], so the answer is 3.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →