【模板】Trie树(基于指针)

本文介绍了使用指针实现的Trie树数据结构,详细阐述了Trie树在字符串处理中的作用,通过实例解析其工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

很常见的字符串处理数据结构……
参考博客:字符串统计神器——Trie树

以前没有怎么认真学过Trie,现在重新打了一遍
用了指针实现,感觉好看多了

#include<cstdio>
const int maxn=100005;
int n,q;
char s[maxn][15];
struct node{
    node *s[26];
    int cnt;
    node () {}
    node (int x):cnt(x) {}
}base[1200005],nil;
typedef node* P_node;
P_node null,len,Rot;
void Trie_init(){
    nil=node(0);null=&nil;len=base;
    for (int i=0;i<26;i++) null->s[i]=null;
}
P_node newnode(){
    *len=node(0);
    for (int i=0;i<26;i++) len->s[i]=null;
    return len++;
}
void ist(P_node &x,char *ch){
    if (x==null) x=newnode();
    x->cnt++;
    if (*ch==0) return;
    ist(x->s[*ch-'a'],ch+1);
}
int pre(P_node &x,char *ch){
    if (*ch==0) return x->cnt;
    return pre(x->s[*ch-'a'],ch+1);
}
int main(){
    Trie_init();Rot=newnode();
    //do something...
    return 0;
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值