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, 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
#include<cstdio>
#include<cstring>
const int mod = 20071027;
const int maxn = 3e5+10;
int len,s,kase,dp[maxn];
char str[maxn],buf[105];
const int maxnode=4000*105;
const int size = 26;
struct Trie{
int ch[maxnode][size];
bool val[maxnode];
int cnt;
Trie() { cnt=1; }
inline void insert(char *s){
int len=strlen(s),u=0;
for( int i=0; i<len; i++ ){
int v=s[i]-'a';
if(!ch[u][v]) ch[u][v]=cnt++;
u=ch[u][v];
}
val[u]=1;
}
inline void clear(){
cnt=1;
memset(ch,0,sizeof(ch));
memset(val,0,sizeof(val));
}
}trie;
int main(){
kase=0;
while(scanf("%s",str)!=EOF){
len=strlen(str);
scanf("%d",&s);
memset(dp,0,sizeof(dp));
trie.clear();
while(s--){
scanf("%s",buf);
trie.insert(buf);
}
dp[len]=1;
for( int i=len-1; i>=0; i-- ){
for( int j=i,u=0; j<len&&trie.ch[u][str[j]-'a']; j++ ){
u=trie.ch[u][str[j]-'a'];
if(trie.val[u]) dp[i]=(dp[i]+dp[j+1])%mod;
}
}
printf("Case %d: %d\n",++kase,dp[0]);
}
return 0;
}