标签:
Description
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.
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 , 1S4000 .
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
Trie上的DP,第一次写trie,发现不能写数组版的啊,比指针版的慢了好多倍。。。。。。。
const int maxv=4e5+3; const ll mod=20071027; char s[maxv]; int S; int idx(char c){return c-‘a‘;} struct trie{ int ch[maxv][27]; bool ma[maxv]; int sz; void clear(){ memset(ch,0,sizeof ch); memset(ma,0,sizeof ma); sz=1; } void insert(char *s){ int n=strlen(s); int u=0,v; for(int i=0;i<n;i++){ v=idx(s[i]); if(ch[u][v]) u=ch[u][v]; else{ ch[u][v]=sz++; u=sz-1; } } ma[u]=1; } }T; char tmp[maxv]; const int maxdp=3e5+30; ll dp[maxdp]; int main(){ int t=0; while(scanf("%s",s)!=EOF){ int ssz=strlen(s); t++; cin>>S; T.clear(); memset(dp,0,sizeof dp); while(S--){ scanf("%s",tmp); T.insert(tmp); } dp[ssz]=1; for(int i=ssz-1;i>=0;i--){////从最后一个字符开始匹配 char *su=&s[i]; int susz=strlen(su); int u=0; for(int j=0;j<susz;j++){ int v=idx(su[j]); if(T.ch[u][v]&&T.ma[T.ch[u][v]]){ dp[i]=(dp[i+j+1]+dp[i])%mod; } if(T.ch[u][v]) u=T.ch[u][v]; else break; } } printf("Case %d: %lld\n",t,dp[0]); } return 0; }
标签:
原文地址:http://www.cnblogs.com/Cw-trip/p/4455421.html