HIHO Trie树入门练习

题目链接

给出很多的串,然后有很多的询问,每次询问需要回答,以这个串为前缀的单词有几个。

把单词建立成字典树,插入的单词的时候,统计每个字符节点经过几次,然后询问的时候,直接查询,没有这个前缀就是0,否则就是f[now]即可

空间的取值,假设n个串,每个长度不大于x,最坏的情况也就是n*x个空间就够了


#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<cstdlib>
#include<vector>
using namespace std;
#define cl(a,b) memset(a,b,sizeof(a))
#define LL long long
#define pb push_back
#define gcd __gcd

#define For(i,j,k) for(int i=(j);i<k;i++)
#define lowbit(i) (i&(-i))
#define _(x) printf("%d\n",x)

const int maxn = 1e6+10;
const int inf  = 1 << 28;


struct Trie{
    int child[maxn][30];
    int f[maxn];
    int root,L;

    int newNode(){
        cl(child[L],-1);
        f[L]=0;
        return L++;
    }

    void init(){
        L = 0;
        root = newNode();
    }

    void insert(const char *s){
        int now = root;
        int len = strlen(s);
        for(int i=0;i<len;i++){
            if(child[now][s[i]-'a']==-1){
                child[now][s[i]-'a']=newNode();
            }
            f[now]++;
            now=child[now][s[i]-'a'];
        }
        f[now]++;
    }

    int query(const char s[]){
        int len = strlen(s);
        int now = root;
        for(int i=0;i<len;i++){
            if(child[now][s[i]-'a']==-1){
                return 0;
            }
            now = child[now][s[i]-'a'];
        }
        return f[now];
    }
}trie;

char tmp[maxn];
int main(){
    int n,m;
    while(~scanf("%d",&n)){
        trie.init();
        for(int i=0;i<n;i++){
            scanf("%s",tmp);
            trie.insert(tmp);
        }
        scanf("%d",&m);
        while(m--){
            scanf("%s",tmp);
            printf("%d\n",trie.query(tmp));
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值