Messy kangaroo babies
Time limit5sMemory limit1024 MB
Given a word S and a list of candidate synonyms, count how many are subsequences of S that can be embedded in at least two different ways.
- Level
Hard8 of 10
- Topics
- String, Dynamic programming, Greedy, Binary search
- Solved
- No attempts yet
Problem
A kangaroo word is a word that carries a synonym of itself (a "baby") in such a way that every letter of the synonym appears in the word, in the same order. For example, pastej is a kangaroo word, because it carries the synonym paj (pastej). aste and atj would also count as babies if we pretend they are words, but paaj and etsa would not. Formally, the baby must be a subsequence of the word.
Furthermore, we say a baby is messy if it fits in the word in two different ways. paj is not a messy baby, but if the original word had been paastej, it would be, since it could then be hidden as either paastej or paastej.
Given a (made-up) word and a list of (made-up) synonyms, how many of the synonyms are messy babies of ?
Input
- The first line contains a non-empty string consisting of the letters
a-z, the word we are asking about. - The second line contains the integer (): the number of synonyms of the word.
- The following lines contain the synonyms, each a non-empty string consisting of the letters
a-z.
No synonym will appear twice, or be equal to .
Let denote the number of letters in , and the sum of the number of letters in the synonyms. Then and .
Output
Print a single integer: the number of words that are messy babies of .
Hint
In sample 1, the first three words are babies of , and also messy. The test case could therefore appear in test group 2 or 4.
In sample 2, the first four words are babies, of which the first two are also messy babies. This test case could not appear in test group 2 or 4.