字典树 (详细)

字典树

Trie字符串统计

维护一个字符串集合,支持两种操作:

  1. I x 向集合中插入一个字符串 x;
  2. Q x 询问一个字符串在集合中出现了多少次。
    共有 N 个操作,输入的字符串总长度不超过 105,字符串仅包含小写英文字母。
    输入格式
    第一行包含整数 N,表示操作数。
    接下来 N 行,每行包含一个操作指令,指令为 I xQ x 中的一种。
    输出格式
    对于每个询问指令 Q x,都要输出一个整数作为结果,表示 x 在集合中出现的次数。
    每个结果占一行。
    数据范围
    1≤N≤2∗104
    输入样例:
5
I abc
Q abc
Q ab
I ab
Q ab

输出样例:

1
0
1

AC代码

#include <iostream>

using namespace std;
const int N = 100010;
int son[N][26], cnt[N], idx;
char str[N];

void insert(char *str){
    int p = 0;
    for (int i = 0; str[i]; i ++ ){
        int u = str[i] - 'a';
        if (!son[p][u]) son[p][u] = ++ idx;//当前结点没有儿子就要建一条路 
        p = son[p][u];//p表示当前分支的最后一个点 
        //cnt[p]++;如果要统计每一个字母出现的次数,则++
    }
    cnt[p] ++ ;//表示以最后这个点结尾的单词数量又多了一个
}

int query(char *str){
    int p = 0;
    for (int i = 0; str[i]; i ++ ){
        int u = str[i] - 'a';
        if (!son[p][u]) return 0;
        p = son[p][u];
    }
    return cnt[p];//遍历结束,返回该单词的个数
}

int main(){
    int n;
    scanf("%d", &n);
    while (n -- )
 {
        char op[2];
        scanf("%s%s", op, str);
        if (*op == 'I') insert(str);
        else printf("%d\n", query(str));
    }

    return 0;
}

01字典树

将一个数的二进制看成一个字符串,这样可以建立一颗字符集为{0,1}的Trie树
01字典树的作用:维护异或极值,维护异或和
01字典树支持修改(删除+重新插入),全局加1(使其维护的所有数值+1)持
如果需要维护异或和时,需要按值从低位到高位建立Trie树

最大异或对

在给定的 N 个整数 A1,A2……AN 中选出两个进行 xor(异或)运算,得到的结果最大是多少?
输入格式
第一行输入一个整数 N。
第二行输入 N 个整数 A1~AN。
输出格式
输出一个整数表示答案。
数据范围
1≤N≤1e5,
0≤Ai<2^31
输入样例:

3
1 2 3

输出样例:

3

AC代码

#include <iostream>
#include <algorithm>

using namespace std;
const int N = 100010, M = 3100010;
int n;
int a[N], son[M][2], idx;

void insert(int x){
    int p = 0;
    for (int i = 30; i >= 0; i -- ){
        int &s = son[p][x >> i & 1];
        if (!s) s = ++ idx;
        p = s;
    }
}

int search(int x){
    int p = 0, res = 0;
    for (int i = 30; i >= 0; i -- ){
        int s = x >> i & 1;
        if (son[p][!s]){
            res += 1 << i;
            p = son[p][!s];
        }
        else p = son[p][s];
    }
    return res;
}

int main(){
    scanf("%d", &n);
    for (int i = 0; i < n; i ++ ){
        scanf("%d", &a[i]);
        insert(a[i]);
    }

    int res = 0;
    for (int i = 0; i < n; i ++ ) res = max(res, search(a[i]));

    printf("%d\n", res);

    return 0;
}
  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Trie树是一种树形数据结构,它主要用于字符串的存储和查找。Trie树的每个节点代表一个字符,根节点不存储任何字符,其他节点代表一个字符串的一部分。在Trie树中,如果一个节点的所有子节点都是叶节点,则该节点表示一个完整的字符串。 下面是一段Java代码,实现了一个简单的Trie树: ``` class TrieNode { private TrieNode[] children = new TrieNode[26]; private boolean isEnd; public TrieNode() {} public TrieNode[] getChildren() { return children; } public void setEnd() { isEnd = true; } public boolean isEnd() { return isEnd; } } class Trie { private TrieNode root; public Trie() { root = new TrieNode(); } public void insert(String word) { TrieNode node = root; for (int i = 0; i < word.length(); i++) { int j = word.charAt(i) - 'a'; if (node.getChildren()[j] == null) { node.getChildren()[j] = new TrieNode(); } node = node.getChildren()[j]; } node.setEnd(); } public boolean search(String word) { TrieNode node = root; for (int i = 0; i < word.length(); i++) { int j = word.charAt(i) - 'a'; if (node.getChildren()[j] == null) { return false; } node = node.getChildren()[j]; } return node.isEnd(); } public boolean startsWith(String prefix) { TrieNode node = root; for (int i = 0; i < prefix.length(); i++) { int j = prefix.charAt(i) - 'a'; if (node.getChildren()[j] == null) { return false; } node = node.getChildren()[j]; } return true; } } ``` 该代码实现了一个Trie树,其中TrieNode ### 回答2: Trie树,也叫字典树或前缀树,是一种用于高效存储和检索字符串的数据结构。下面是使用Java编写的一个简单Trie树的代码示例: ```java class TrieNode { private TrieNode[] children; private boolean isEndOfWord; public TrieNode() { children = new TrieNode[26]; // 26个字母 isEndOfWord = false; } } class Trie { private TrieNode root; public Trie() { root = new TrieNode(); } public void insert(String word) { TrieNode current = root; for (int i = 0; i < word.length(); i++) { char ch = word.charAt(i); int index = ch - 'a'; if (current.children[index] == null) { current.children[index] = new TrieNode(); } current = current.children[index]; } current.isEndOfWord = true; } public boolean search(String word) { TrieNode current = root; for (int i = 0; i < word.length(); i++) { char ch = word.charAt(i); int index = ch - 'a'; if (current.children[index] == null) { return false; } current = current.children[index]; } return current.isEndOfWord; } public boolean startsWith(String prefix) { TrieNode current = root; for (int i = 0; i < prefix.length(); i++) { char ch = prefix.charAt(i); int index = ch - 'a'; if (current.children[index] == null) { return false; } current = current.children[index]; } return true; } } ``` 在上述代码中,我们使用了两个类:TrieNode和Trie。TrieNode表示Trie树中的每个节点,包含一个26个元素的数组来存储子节点和一个布尔变量isEndOfWord来表示当前节点是否是一个单词的结尾。Trie类是Trie树的实现,包含了插入、搜索和前缀搜索三个方法。 在插入方法中,我们首先从根节点开始,遍历插入的字符串的每个字符。通过计算字符在字母表中的位置,我们可以将其作为TrieNode数组的索引,以此构造Trie树的路径。最后,将叶子节点的isEndOfWord设置为true,表示该路径对应的字符串在Trie树中存在。 在搜索方法中,我们同样从根节点开始,遍历待搜索的字符串的每个字符。如果遇到某个字符在Trie树当前节点的子节点中不存在,则说明该字符串不存在于Trie树中,返回false。如果成功遍历完所有字符,并且叶子节点对应的isEndOfWord为true,则说明该字符串存在于Trie树中,返回true。 在前缀搜索方法中,与搜索方法类似,只是在遍历完所有字符后不进行isEndOfWord的判断,始终返回true。这是因为前缀搜索只需要判断给定字符串的前缀是否存在于Trie树中,无需判断是否是一个完整单词。 通过上述样例代码,我们可以实现一个简单的Trie树,并且能够进行插入、搜索和前缀搜索等操作。Trie树的优势在于其高效的字符串存储和检索性能,特别适用于需要进行前缀搜索的场景,如自动补全、拼写纠错等应用。 ### 回答3: Trie树(也称为字典树或前缀树)是一种树形数据结构,用于高效地存储和搜索字符串集合。下面是用Java编写的简单Trie树代码段: ```java class TrieNode { private TrieNode[] children; private boolean isEndOfWord; public TrieNode() { children = new TrieNode[26]; // 26个小写字母 isEndOfWord = false; } } public class Trie { private TrieNode root; public Trie() { root = new TrieNode(); } public void insert(String word) { TrieNode currentNode = root; for (int i = 0; i < word.length(); i++) { int index = word.charAt(i) - 'a'; if (currentNode.children[index] == null) { currentNode.children[index] = new TrieNode(); } currentNode = currentNode.children[index]; } currentNode.isEndOfWord = true; } public boolean search(String word) { TrieNode currentNode = root; for (int i = 0; i < word.length(); i++) { int index = word.charAt(i) - 'a'; if (currentNode.children[index] == null) { return false; } currentNode = currentNode.children[index]; } return currentNode != null && currentNode.isEndOfWord; } public boolean startsWith(String prefix) { TrieNode currentNode = root; for (int i = 0; i < prefix.length(); i++) { int index = prefix.charAt(i) - 'a'; if (currentNode.children[index] == null) { return false; } currentNode = currentNode.children[index]; } return true; } } ``` 上述代码实现了一个简单的Trie树。Trie树的核心部分是`TrieNode`类,每个节点包含一个长度为26的子节点数组`children`,以及一个`boolean`类型的`isEndOfWord`字段,表示当前节点是否为一个单词的结尾。 `Trie`类是Trie树的主要类,主要提供插入、搜索和前缀搜索功能。在插入操作中,我们从根节点开始遍历字符串的每个字符,并根据字符的ASCII值索引到对应的子节点位置。如果当前节点的子节点为空,则创建一个新节点,并将当前节点更新为新节点。插入完成后,我们将最后一个节点的`isEndOfWord`字段设置为`true`,表示一个单词的结尾。 在搜索操作中,我们也是从根节点开始遍历字符串的每个字符,并根据字符的ASCII值索引到对应的子节点位置。如果遍历过程中发现某个字符的子节点为空,则返回`false`,表示找不到对应的单词。最后,我们检查最后一个字符的节点是否为空,并查看其`isEndOfWord`字段是否为`true`,来确定搜索操作的结果。 在前缀搜索操作中,逻辑与搜索操作类似,只是在找到对应的子节点位置时不需要判断`isEndOfWord`字段,只需保证一直存在就行。 通过这样的Trie树数据结构,我们可以高效地存储和搜索大量的字符串集合,具有较低的时间复杂度。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值