zoukankan      html  css  js  c++  java
  • 字典树

    #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
    ",++cas,dp[0]);
        }
    }
  • 相关阅读:
    页码数求0到9共有多少个
    reduce
    map,filter
    匿名函数 lambda
    递归
    python 切片
    函数
    集合(set)
    python 中random 库的使用
    printf 输出?
  • 原文地址:https://www.cnblogs.com/033000-/p/10317576.html
Copyright © 2011-2022 走看看