指针版

#define MAXNUM 26
//定义字典树结构体
typedef struct Trie
{
bool flag;//从根到此是否为一个单词
Trie *next[MAXNUM];
}Trie;
//声明一个根
Trie *root;
//初始化该根
void init()
{
root = (Trie *)malloc(sizeof(Trie));
root->flag=false;
for(int i=;i<MAXNUM;i++)
root->next[i]=NULL;
}
//对该字典树的插入单词操作
void insert(char *word)
{
Trie *tem = root;
while(*word!='\0')
{
if(tem->next[*word-'a']==NULL)
{
Trie *cur = (Trie *)malloc(sizeof(Trie));
for(int i=;i<MAXNUM;i++)
cur->next[i]=NULL;
cur->flag=false;
tem->next[*word-'a']=cur;
}
tem = tem->next[*word-'a'];
word++;
}
tem->flag=true;
}
//查询一个单词的操作
bool search(char *word)
{
Trie *tem = root;
for(int i=;word[i]!='\0';i++)
{
if(tem==NULL||tem->next[word[i]-'a']==NULL)
return false;
tem=tem->next[word[i]-'a'];
}
return tem->flag;
}
//释放字典树内存操作,由于本题测试数据后程序自动跳出,所以这里没写释放内存函数
void del(Trie *cur)
{
for(int i=;i<MAXNUM;i++)
{
if(cur->next[i]!=NULL)
del(cur->next[i]);
}
free(cur);
}

01Trie

 struct Trie
{
int root, tot, next[*][], cnt[*], end[*]; inline int Newnode()
{
memset(next[tot], -, sizeof(next[tot]));
cnt[tot] = ;
end[tot] = ;
return tot ++;
} inline void Init()
{
tot = ;
root = Newnode();
} inline void Insert(int x)
{
int p = root;
cnt[p] ++;
for(int i = ; i >= ; i --)
{
int idx = (x >> i) & ;
if(next[p][idx] == -)
next[p][idx] = Newnode();
p = next[p][idx];
cnt[p] ++;
}
end[p] = x;
} inline void Del(int x)
{
int p = root;
cnt[p] --;
for(int i = ; i >= ; i --)
{
int idx = (x >> i) & ;
p = next[p][idx];
cnt[p] --;
}
} inline int Search(int x) //求x的异或最大值,看情况修改
{
int p = root;
for(int i = ; i >= ; i --)
{
int k = (x >> i) & ;
if(next[p][k^] != - && cnt[next[p][k^]])
p = next[p][k^];
else
p = next[p][k];
}
return x ^ end[p];
}
}tr;

Trie树 & 01Trie的更多相关文章

  1. Luogu P2922 [USACO08DEC]秘密消息Secret Message 字典树 Trie树

    本来想找\(01Trie\)的结果找到了一堆字典树水题...算了算了当水个提交量好了. 直接插入模式串,维护一个\(Trie\)树的子树\(sum\)大小,求解每一个文本串匹配时走过的链上匹配数和终点 ...

  2. Trie树入门

    Trie树入门 貌似很多人会认为\(Trie\)是字符串类型,但是这是数据结构!!!. 详情见度娘 下面开始进入正题. PS:本文章所有代码未经编译,有错误还请大家指出. 引入 先来看一个问题 ​ 给 ...

  3. 【bzoj3217】ALOEXT 替罪羊树套Trie树

    题目描述 taorunz平时最喜欢的东西就是可移动存储器了……只要看到别人的可移动存储器,他总是用尽一切办法把它里面的东西弄到手. 突然有一天,taorunz来到了一个密室,里面放着一排可移动存储器, ...

  4. Trie树(小)总结 By cellur925

    关于\(Trie\)树的详细介绍,还请移步这篇深度好文 基本操作 插入 void insert() { int p=0; int len=strlen(tmp+1); for(int i=1;i< ...

  5. BZOJ 4260: Codechef REBXOR (trie树维护异或最大值)

    题意 分析 将区间异或和转化为前缀异或和.那么[L,R][L,R][L,R]的异或和就等于presum[R] xor presum[L−1]presum[R]\ xor \ presum[L-1]pr ...

  6. [CSP-S模拟测试]:big(Trie树+贪心)

    题目描述 你需要在$[0,2^n)$中选一个整数$x$,接着把$x$依次异或$m$个整数$a_1~a_m$.在你选出$x$后,你的对手需要选择恰好一个时刻(刚选完数时.异或一些数后或是最后),将$x$ ...

  7. 【题解】HDU5845 Best Division (trie树)

    [题解]HDU5845 Best Division (trie树) 题意:给定你一个序列(三个参数来根),然后请你划分子段.在每段子段长度小于等于\(L\)且子段的异或和\(\le x\)的情况下最大 ...

  8. The XOR Largest Pair (trie树)

    题目描述 在给定的 NN 个整数 A_1,A_2,--,A_NA1​,A2​,--,AN​ 中选出两个进行xor运算,得到的结果最大是多少?xor表示二进制的异或(^)运算符号. 输入格式 第一行输入 ...

  9. 基于trie树做一个ac自动机

    基于trie树做一个ac自动机 #!/usr/bin/python # -*- coding: utf-8 -*- class Node: def __init__(self): self.value ...

随机推荐

  1. java之Class类详解

    测试中需要用到的代码 InterfaceA代码: package jichu; interface InterfaceA { String s1 = "this is s1 in Inter ...

  2. C语言回滚(一)

    //用循环计算输入的字符数 #include<stdio.h> #include<string.h> #include<stdlib.h> int main(){ ...

  3. PHP学习笔记:使用session来存储用户的登录信息

    session可以用来存储多种类型的数据,因此具有很多的用途,常用来存储用户的登录信息,购物车数据,或者一些临时使用的暂存数据等. 用户在登录成功以后,通常可以将用户的信息存储在session中,一般 ...

  4. rabbitmq学习笔记2 基本概念

    官网:http://www.rabbitmq.com 参考:http://blog.csdn.net/column/details/rabbitmq.html   1 基本概念 rabbitmq se ...

  5. 【GOF23设计模式】责任链模式

    来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_责任链模式.公文审批.供应链系统的采购审批.异常链.过滤器和拦截器调用过程 package com.test.chainO ...

  6. 帝吧出征FB:这李毅吧的“爆吧”文化是如何形成的

    声明:本文不对爆吧行为及其涉及的事件进行是非判断,只探讨帝吧文化本身,欢迎拍砖.更正和补充. 一.“帝吧FB出征”事件梳理 继上次全网集体骂 “薯片”事件后,昨日(1月20日)晚7点,又发生了一次互联 ...

  7. UIStoryBoard 中修改控件borderColor

    storyBoard中直接修改UI控件的边框颜色,可以避免先关联再从控制器中修改属性等繁琐操作 因为layer.borderColor隶属于CALayer对象,且CGColor无法直接在sb中直接使用 ...

  8. Apple Pay(转)

    Apple Pay 是在 iOS 8 中第一次被介绍,它可以为你的应用中的实体商品和服务,提供简单.安全.私密的支付方式.它使得用户支付起来非常简便,只需按一下指纹就可以授权进行交易. Apple P ...

  9. sql 存储过程中top 后面跟参数的问题

    之前存储过程中有top的情况,都是拼接sql,然后通过exec执行,进行查询结果,很不方便. 今天研究了,原来top后面是可以直接写参数的. 只需要top 后面的参数加上小括号就好了 eg: TOP ...

  10. paas架构之docker——镜像管理

    1. 镜像管理 1.1. 列出镜像 Sudo docker images 1.2. 查看镜像 Sudo docker images xxxx 1.3. 拉取镜像 Sudo docker pull ub ...