Problem 956
Number of Ways to Form a Target String Given a Dictionary
Hard · 30 pts · String, Dynamic Programming
Problem
All dictionary words have the same length. Build TARGET from left to right: to place TARGET[i], choose some word and take its character at column k, after which only columns greater than k may be used for the next characters (from any words). Count the ways modulo 1000000007. Count how many words have each letter in each column; then ways[i][k] = ways[i][k + 1] + count[k][TARGET[i]] × ways[i + 1][k + 1]. Keep the counts in a 64-bit integer type (long long in C++, long in Java) and take the remainder after every addition and multiplication: adding or multiplying two remainders can be larger than a 32-bit int can hold (Python needs nothing special).
Input
The first line contains N. The second line contains the N words. The third line contains TARGET. All are lowercase letters.
Output
Print the number of ways modulo 1000000007.
Constraints
1 <= N <= 1000; 1 <= word length <= 1000; 1 <= |TARGET| <= 1000
Example input
3 acca bbbb caca aba
Example output
6
Explanation
With words acca bbbb caca and target aba, there are 6 ways, for example taking a from column 0 of acca, b from column 1 of bbbb and a from column 3 of caca.