6872.Word Combinations

Time Limit: 1s Memory Limit: 512MB

You are given a string of length $n$ and a dictionary containing $k$ words. In how many ways can you create the string using the words?

Input Format(From the terminal/stdin)

The first input line has a string containing $n$ characters between a–z.

The second line has an integer $k$ : the number of words in the dictionary.

Finally there are $k$ lines describing the words. Each word is unique and consists of characters a–z.

  • $1 \le n \le 5000$
  • $1 \le k \le 10^5$
  • the total length of the words is at most $10^6$

Output Format(To the terminal/stdout)

Print the number of ways modulo $10^9+7$ .

Sample Input

Copy
ababc
4
ab
abab
c
cb
     \n
 \n
  \n
    \n
 \n
  \n

Sample Output

Copy
2
 \n

The possible ways are ab+ab+c and abab+c .

Source: CSES, String Algorithms, 1731

Submit

请先 登录

© 2025 FAQs