1 #include<iostream> 2 #include<stdio.h> 3 #include<string.h> 4 #include<algorithm> 5 #include<stdlib.h> 6 #include<math.h> 7 #include<queue> 8 #include<vector> 9 #include<map> 10 11 const int maxnode=501000; 12 const int maxn=300100; 13 const int mod=20071027; 14 15 using namespace std; 16 17 int n,cas,d[maxn]; 18 char s[maxn]; 19 struct Trie 20 { 21 int ch[maxnode][26]; 22 bool val[maxnode]; 23 int sz; 24 Trie(){sz=1;memset(ch[0],0,sizeof(ch[0]));} 25 void reset(){sz=1;memset(ch[0],0,sizeof(ch[0]));} 26 int idx(char x){return x-'a';} 27 void insert(char *nw,int v) 28 { 29 int u=0,l=strlen(nw); 30 for(int i=0;i<l;i++) 31 { 32 int x=idx(nw[i]); 33 if (!ch[u][x]){ 34 memset(ch[sz],0,sizeof(ch[sz]));//边更新,边初始化 35 val[sz]=0; 36 ch[u][x]=sz++; 37 } 38 u=ch[u][x]; 39 } 40 val[u]=v; 41 } 42 int search(int st)//查找s[i...len-1]的所有前缀 43 { 44 int u=0,l=strlen(s); 45 int ans=0; 46 for(int i=st;i<l;i++) 47 { 48 int x=idx(s[i]); 49 if (!ch[u][x]) return ans; 50 u=ch[u][x]; 51 if(val[u])ans=(ans+d[i+1])%mod;//i+1是计算化简后的,记住标记 52 } 53 return ans; 54 } 55 }tree;//tree的定义要放到main的外面 56 57 int main() 58 { 59 cas=0; 60 while(scanf("%s",s)==1) 61 { 62 scanf("%d",&n); 63 cas++; 64 tree.reset(); 65 char word[maxn]; 66 for(int i=0;i<n;i++) 67 { 68 cin>>word;tree.insert(word,1); 69 } 70 int len=strlen(s); 71 memset(d,0,sizeof(d)); 72 d[len]=1; 73 for(int i=len-1;i>=0;i--) 74 d[i]=tree.search(i); 75 printf("Case %d: %d ",cas,d[0]); 76 // cout<<"Case "<<cas<<": "<<d[0]<<endl; 77 } 78 return 0; 79 }