#K16341. Word Break Count
Word Break Count
Word Break Count
You are given a non-empty string s
and a dictionary of words. Your task is to count the number of ways to partition the string s
such that every substring in the partition is a valid word in the dictionary.
We can solve this problem using dynamic programming. Let \(dp[i]\) represent the number of ways to partition the substring s[0:i]
. The recurrence is given by:
\(dp[i] = \sum_{j=0}^{i-1} dp[j]\) if \(s[j:i] \in \text{wordDict}\), with the base case \(dp[0] = 1\).
Output the final result \(dp[n]\) where \(n\) is the length of s
.
inputFormat
The input is read from stdin and has the following format:
- The first line contains the string
s
. - The second line contains an integer
n
, representing the number of words in the dictionary. - The following
n
lines each contain one word from the dictionary.
outputFormat
Print a single integer to stdout that represents the number of ways to partition the string s
using words from the dictionary.
catsanddog
5
cat
cats
and
sand
dog
2