String Paradigm
Practice
3 (1 votes)
Algorithms
Dynamic programming
Dynamic programming
Math
Medium
Two dimensional
Problem
58% Success 547 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

You are given a string $$S$$ of length $$N$$ having distinct lowercase alphabetic characters. Each character in the string $$S$$ has some follow-up characters. The follow-up character denotes what characters can follow a particular character that belongs to the string $$S$$. Note that the follow-up characters belong to the string $$S$$.

Now you have to pick one of the characters of the string $$S$$ and mark it as a start character. After you pick one character, you need to look in its follow-up list and choose one character from it and continue this to form a new string.

Now, you have to count the total number of non-empty subsequences of all the possible $$M$$ length strings that can be made out of the characters of the string $$S$$. Since the number could be very large output it modulo $$10^9 + 9$$.

Note: Carefully check out the sample explanation for a better understanding of the problem.

Input Format

The first line of the input contains a single integer $$N$$, the length of the string.
The second line of the input contains a string $$S$$ of length $$N$$.
Then $$N$$ lines follow, $$i$$-th $$(1 \le i \le N)$$ line contains a string $$T_i$$ where each character in the string is a follow up character of the $$i$$-th character in the main string $$S$$.
The last line contains a single integer $$M$$, length of the string that has to be constructed.

Output Format

The only line of the output contains a single integer, the total number of non-empty subsequences of all the possible $$M$$ length strings modulo $$10^9 + 9$$.

Constraints

$$1 \le N \le 10$$
$$1 \le |T_i| \le N$$
$$1 \le M \le 5$$ x $$10^5$$

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
1 votes
Tags:
Dynamic Programming2D dynamic programmingAlgorithms
Points:30
23 votes
Tags:
AlgorithmsApprovedDynamic ProgrammingMediumOpenTwo dimensional
Points:30
3 votes
Tags:
Dynamic ProgrammingC++2D dynamic programmingAlgorithms