#include <cstdio>#include <iostream>#include <algorithm>#include <queue>#include <cmath>#include <cstring>#include <stack>#include <set>#include <map>#include <vector>using namespace STD;#define INF 0X2FFFFFFF#define LL Long Long#define MAX (A, B) ((a) > (a))? (a):(B)#define MIN (a) < (b))? (a):(B)#define ID (a) ((A-' a '))Chars[300005];intSzConst intMaxnode =4000* -+ -;intch[maxnode][ -];intVal[maxnode];intdp[300005];voidInsertChar*s) {intn =strlen(s);intU =0; for(inti =0; i < n;i++) {intc = ID (s[i]);if(!ch[u][c]) {Val[sz] =0;memset(Ch[sz],0,sizeof(Ch[sz])); CH[U][C] = sz + +; } u = Ch[u][c]; } Val[u] =1;}intQueryintLocintc) {intU =0;intn = loc + C; for(inti = Loc;i <= n;i++) {intc = ID (s[i]);if(!ch[u][c]) {return false; } u = Ch[u][c]; }returnVal[u];}intMain () {intCC =1; while(scanf('%s ', s)!=eof) {intTintLen =strlen(s);memset(ch[0],0,sizeof(ch[0]));memset(DP,0,sizeof(DP)); SZ =1;scanf("%d", &t);Chara[ $]; while(t--) {scanf('%s ', a); Insert (a); } Dp[len] =1; for(intI= len-1; I >=0; i--) { for(intj =0; J < -&& J+i < len;j++) {if(Query (I,J)) {Dp[i] + = dp[i+j+1]; Dp[i]%=20071027; } } }printf("Case%d:%d\n", cc++,dp[0]); }return 0;}
The beginning of the use of memory search time-out, replaced by DP can be passed, DP is non-recursive memory search!
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
uva3942 Dictionary tree plus DP