【UVA】Remember the Word(Trip树)

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值