Remember the Word - UVaLive 3942 Trie树+dp
2014-08-21 21:49
295 查看
Neal 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.
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.
题意:问你原串由下面的子串拼接而成的方式有多少种。
思路:之前做过一道差不多的,UVaLive2426/article/1969281.html。那道用的是KMP+dp。但是正确的做法是Trie树,这道用KMP会T。
AC代码如下:
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
题意:问你原串由下面的子串拼接而成的方式有多少种。
思路:之前做过一道差不多的,UVaLive2426/article/1969281.html。那道用的是KMP+dp。但是正确的做法是Trie树,这道用KMP会T。
AC代码如下:
#include<cstdio> #include<cstring> using namespace std; int dp[301010],MOD=20071027,n,m; char s[301010],s2[1100]; struct Trie { int index; Trie *next[26]; Trie() { index=-1; memset(next,0,sizeof(next)); } }; Trie *root=new Trie; void Trie_Insert(Trie *tr,char *s) { if(*s!='\0') { if(tr->next[*s-'a']==0) tr->next[*s-'a']=new Trie; Trie_Insert(tr->next[*s-'a'],s+1); } else tr->index=1; } void Trie_Search(Trie *tr,int start,int len) { if(tr->index!=-1) { dp[start+len]+=dp[start]; dp[start+len]%=MOD; } if(start+len>=n) return; if(tr->next[s[start+len]-'a']!=0) Trie_Search(tr->next[s[start+len]-'a'],start,len+1); } int main() { int t=0,i,j,k; while(~scanf("%s",s)) { Trie *root=new Trie; n=strlen(s); scanf("%d",&m); for(i=1;i<=m;i++) { scanf("%s",s2); Trie_Insert(root,s2); } memset(dp,0,sizeof(dp)); dp[0]=1; for(i=1;i<=n;i++) Trie_Search(root,i-1,0); printf("Case %d: %d\n",++t,dp ); } }
相关文章推荐
- UVALive - 3942:Remember the Word
- UVALive 3942 Remember the Word 前缀树Trie
- 【暑假】[实用数据结构]UVAlive 3942 Remember the Word
- UVALive - 3942 Remember the Word[Trie DP]
- uvalive3942 Remember the Word
- UVALIVE 3942 Remember the Word 字典树
- (trie)UVALive - 3942 Remember the Word
- [UVALive3942] Remember the Word && 字符串
- UVA Live-3942 Remember the Word(trie树入门题)
- 【暑假】[实用数据结构]UVAlive 3942 Remember the Word
- uvalive 3942 - Remember the Word(Trie)
- uvalive 3942 Remember the Word 字典树+dp
- UVALive 3942 Remember the Word(trie + dp)
- uva 1401 && Live Archive 3942 Remember the Word
- UVALive 3942 Remember the Word(字典树 + 简单dp)
- UVALive 3942 Remember the Word(字典树+dp)
- 【Uvalive3942】Remember the Word——字典树+dp
- UVALive - 3942 Remember the Word[树状数组]
- UVALive 3942 - Remember the Word(DP,数组Trie+指针Trie)
- UVALive 3942 (LA 3492) Remember the Word Trie树 + 记忆化搜索