码迷,mamicode.com
首页 > 其他好文 > 详细

字典树

时间:2019-01-25 00:18:22      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:col   fine   ==   return   turn   i++   class   ret   ons   

#include<bits/stdc++.h>
using namespace std;
#define IO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
const int maxnode=700000+5;
const int sigma_size=26;
const int mod=20071027;
char tmp[200+5];
char st[maxnode];
int dp[maxnode];
int ch[maxnode][sigma_size];
int val[maxnode];
struct Trie
{

    int sz;
    Trie()
    {
        sz=1;memset(ch[0],0,sizeof(ch[0]));
    }
    int idx(char c){return c-a;}
    void Insert(char *s,int v)
    {
        int u=0,n=strlen(s);
        for(int i=0;i<n;i++){
            int c=idx(s[i]);
            if(!ch[u][c]){
                memset(ch[sz],0,sizeof(ch[sz]));
                val[sz]=0;
                ch[u][c]=sz++;
            }
            u=ch[u][c];
        }
        val[u]=v;
    }
    int Find(char *s,int pos)
    {
        int ans=0;
        int u=0,n=strlen(s);
        for(int i=pos;i<n&&i<=pos+100;i++){
            int c=idx(s[i]);
            if(!ch[u][c]) return ans;
            u=ch[u][c];
            if(val[u])
                ans=(dp[i+1]+ans)%mod;
        }
        return ans;
    }

};
Trie T;
int32_t main()
{
    int n,cas=0;
    while(scanf("%s",st)==1){
        T=Trie();
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            scanf("%s",tmp);
            T.Insert(tmp,1);
        }
        int len=strlen(st);
        dp[len]=1;
        for(int i=len-1;i>=0;i--){
            dp[i]=T.Find(st,i);
        }
        printf("Case %d: %d\n",++cas,dp[0]);
    }
}

 

字典树

标签:col   fine   ==   return   turn   i++   class   ret   ons   

原文地址:https://www.cnblogs.com/033000-/p/10317576.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!