1401 - Remember the Word
Time limit: 3.000 secondsNeal 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 , 1S
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
白书上的例题 ,建Trie树来加速DP , 长姿势了。
将所有字典中的单词插入字典树中,用d[i]表示str[i]到str[n]的所有分解法数。所以有d[i]=sum(i+len(x)),其中x为str[i]为开头的字符串str[i...n],即枚举x,x={str[i]}或{str[i],str[i+1]},........或{str[i]+str[i+1]+str[i+2]+.....+str[n]}.初始化d[n+1]=1。
#include <cstdio> #include <cmath> #include <algorithm> #include <iostream> #include <cstring> #include <map> #include <string> #include <stack> #include <cctype> #include <vector> #include <queue> #include <set> #include <iomanip> using namespace std; //#define Online_Judge #define outstars cout << "***********************" << endl; #define clr(a,b) memset(a,b,sizeof(a)) #define lson l , mid , rt << 1 #define rson mid + 1 , r , rt << 1|1 #define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++) #define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++) #define REP(i , x , n) for(int i = (x) ; i > (n) ; i--) #define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--) #define mk make_pair const int inf = 1 << 30; const int MAXN = 500000 + 50; const int maxw = 100 + 20; const int MAXNNODE = 1000 +10; const long long LLMAX = 0x7fffffffffffffffLL; const long long LLMIN = 0x8000000000000000LL; const int INF = 0x7fffffff; const int IMIN = 0x80000000; #define eps 1e-8 #define mod 20071027 typedef long long LL; const double PI = acos(-1.0); typedef double D; typedef pair<int , int> pii; const D e = 2.718281828459; int d[MAXN] , num , begin; struct Trie { int ch[MAXN][30]; int val[MAXN]; int sz; }Trie; int idx(char c) { return c - 'a'; } void init() { Trie.sz = 1; clr(Trie.ch[0] , 0); } void insert(char * s) { int u = 0 , n = strlen(s); FOR(i , 0 , n) { int c = idx(s[i]); if(!Trie.ch[u][c]) { clr(Trie.ch[Trie.sz] ,0); Trie.val[Trie.sz] = 0; Trie.ch[u][c] = Trie.sz++; } u = Trie.ch[u][c]; } Trie.val[u] = 1; } void find(char * s) { int u = 0; for(int i = 0 ; s[i] ; i++) { int c = idx(s[i]); if(!Trie.ch[u][c])return ; u = Trie.ch[u][c]; if(Trie.val[u])num = (num + d[begin + 1 + i]) % mod; } } char str[MAXN] , s[4200]; int main() { //ios::sync_with_stdio(false); #ifdef Online_Judge freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); #endif // Online_Judge int kase = 1; while(~scanf("%s" , str)) { printf("Case %d: " , kase ++); int n; init(); scanf("%d" , &n); while(n --) { scanf("%s" ,s); insert(s); } int len = strlen(str); d[len] = 1; REPP(i , len - 1 , 0) { num = 0; begin = i; find(&str[i]); d[i] = num; } printf("%d\n" , d[0]); } return 0; }