#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 333333;
const int sigma_size = 28;
const int MOD = 20071027;
const int MAXD = 111;
char str[maxn];
int Case = 1;
struct tr{
int ch[4010 * 100][sigma_size];
int val[maxn];
int cnt;
char idx(char c){
return c - 'a';
}
void init(){
cnt = 1;
memset(ch,0,sizeof(ch));
}
void insert(char *str,int num){
int u = 0;
for(int i = 0 ; i < strlen(str) ; i++){
int x = idx(str[i]);
if(!ch[u][x]){
memset(ch[cnt],0,sizeof(ch[cnt]));
val[cnt] = 0;
ch[u][x] = cnt ++;
}
u = ch[u][x];
}
val[u] = num;
}
int find(char *str){
int u = 0;
for(int i = 0; i < strlen(str) ; i ++){
int x = idx(str[i]);
if(!ch[u][x]) return 0;
u = ch[u][x];
}
return val[u];
}
}Trip;
void read_case(){
int n;
scanf("%d",&n);
for(int i = 1 ; i <= n ; i++){
char _str[MAXD];
scanf("%s",_str);
Trip.insert(_str,1);
}
return ;
}
void solve(){
read_case();
int dp[maxn];
memset(dp,0,sizeof(dp));
dp[0] = 1;
int len = strlen(str + 1);
for(int i = 0 ; i <= len ; i++){
int u = 0;
for(int j = 1; i + j <= len ; j ++){
int x = Trip.idx(str[i + j]);
u = Trip.ch[u][x]; //下一个结点的编号
if(!u) break;
else if(Trip.val[u] == 1){
dp[i + j] += dp[i];
if(dp[i+j] >= MOD)
dp[i+j] %= MOD;
}
}
}
printf("Case %d: %d\n",Case ++,dp[len]);
}
int main(){
while(scanf("%s",str + 1) != EOF){
Trip.init();
solve();
}
return 0;
}
【UVA】Remember the Word(Trip树)
最新推荐文章于 2020-08-29 18:40:39 发布