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 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.
For each test case, output the number, as described above, from the task description modulo 20071027.
abcd 4 a b cd ab
Case 1: 2
#include <iostream> #include <cstring> #include <cstdio> #include <vector> using namespace std; char str[333333]; char words[4444][111]; int f[333333]; //用类,或者结构体定义都行 struct trie { trie* next[26]; int num; int value; trie() { for(int i=0;i<26;i++) next[i]=NULL; value=0;//记录是不是一个单词 num=0;//记录单词出现的次数 } void clear() { for(int i=0;i<26;i++) next[i]=NULL; value=0;//记录是不是一个单词 num=0;//记录单词出现的次数 } }root; //插入: void insert(char* s) { trie* p=&root; int k=0; while(s[k]!='\0') { if(!p->next[s[k]-'a']) p->next[s[k]-'a']=new trie; p=p->next[s[k]-'a']; p->num++; k++; } p->value=1; } //查找 void find(char* s,int pos) { trie* p=&root; int k=0; while(s[k]!='\0'&&p->next[s[k]-'a']) { p=p->next[s[k]-'a']; if (p->value==1) { f[pos+k+1]=(f[pos+k+1]+f[pos])%20071027; } k++; } } int main() { int l,s; int cnt=0; while (~scanf("%s",str+1)) { cnt++; memset(f,0,sizeof(f)); root.clear(); scanf("%d",&s); l=strlen(str+1); for (int i=0;i<s;i++) { scanf("%s",words[i]); insert(words[i]); } f[0]=1; for (int i=1;i<=l;i++) { if (f[i-1]) { find(str+i,i-1); } } printf("Case %d: %d\n",cnt,f[l]); } return 0; }