https://www.cnblogs.com/wangdongkai/p/5738620.html
DescriptionNeal is very curious about combinatorial problems, and now here comes a problem about words. Knowing that Ray has a photographic memory and this may not trouble him, Neal gives it to Jiejie. Since Jiejie can’t remember numbers clearly, he just uses sticks to help himself. Allowing for Jiejie’s only 20071027 sticks, he can only record the remainders of the numbers divided by total amount of sticks. The problem is as follows: a word needs to be divided into small pieces in such a way that each piece is from some given set of words. Given a word and the set of words, Jiejie should calculate the number of ways the given word can be divided, using the words in the set.
Input
The input file contains multiple test cases. For each test case: the first line contains the given word whose length is no more than 300 000. The second line contains an integer S, 1 ≤ S ≤ 4000. Each of the following S lines contains one word from the set. Each word will be at most 100 characters long. There will be no two identical words and all letters in the words will be lowercase. There is a blank line between consecutive test cases. You should proceed to the end of file.
Output
For each test case, output the number, as described above, from the task description modulo 20071027.
Sample Input
abcd
4
a
b
cd
ab
Sample Output
Case 1: 2
题意:给你一个长字符串,和一些短字符串,问你用短字符串拼成长字符串的方案数
分析:很明显的dp题。定义状态dp[i]表示从i开始的字符串(后缀)的方案数,则转移方程就是dp[i]+=dp[i+len(x)],x是从i开始的字符串的前缀。
接下来只要求出所有的前缀的长度就行了,枚举肯定会超时,考虑用字典树来做,将短的字符串按字典树建树,单词节点保存单词的长度和是否为单词的标记,每次去查找就行了。
for(int i=len-1;i>=0;i--) { tot=0; memset(l,0,sizeof(l)); //cout<<str+i<<endl; Trie_search(root,str+i); for(int j=1;j<=tot;j++) { dp[i]+=dp[i+l[j]]; dp[i]%=MOD; } } printf("Case %d: %d\n",iCase,dp[0]);
dp[i]表示以i开始(即后缀[i,L])的字符串的分解方案数
dp[i]=Σ dp[i+len[x]] (x是[i,L]的前缀)
判断x是否是[i,L]的前缀:
构造字典树,
如果在i发现了单次结尾,dp[]+=dp[i+1],不需要字典树每个节点挂一个链表
https://www.edwardzcn98yx.com/2018/08/10/【UVALive-3942】解题报告(字典树,dfs