284. Grammar
Time limit per test: 2 seconds
Memory limit: 64 megabytes
input: standard
output: standard
Consider an alphabet consisting of terminal characters 'a' and 'b' and non-terminal characters
. Each non-terminal character
has a description, which is a string of terminal and non-terminal
characters; the non-terminal characters in the description are less
than
. The description may be empty.
You take the string containing single non-terminal character
and replace all non-terminal characters in it by their definitions
until we obtain a string containing only terminal characters, which is
called the final string.
The task is: given a nonempty string
of terminal characters, determine the number of its occurences in the final string.
Input
The first line of the input contains single integer
(
). The second line contains string
containing at most 100 characters. The rest of the input contains the non-terminal characters' descriptions. The
-nd line (
) contains the description of
-th character. It starts with a non-negative integer
, followed by
characters of the alphabet separated by spaces. The sum of all
does not exceed 500.
Output
The only line of the output must contain the answer without leading zeroes.
Example(s)
sample input
|
sample output
|
2
abb
2 a b
3 a 1 b
|
1
|
Novosibirsk SU Contest #2, by Novosibirsk Team #1