【算法基础】2.Trie树、Trie字符串统计、最大异或对(内含模板)

1. Trie树

用于高效地存储和查找"字符串集合"的结构。字符的类型是相同的,同为小写或大写或数字

1. Trie树的构建

【算法基础】2.Trie树、Trie字符串统计、最大异或对(内含模板)_第1张图片

2.Trie树的查找

按照分支走,这里不多赘述。

3. 例题1: Trie字符串统计

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

1. “I x”向集合中插入一个字符串x;
2. “Q x”询问一个字符串在集合中出现了多少次。
共有N个操作,输入的字符串总长度不超过 105,字符串仅包含小写英文字母。

题解:

#include 
using namespace std;
const int N=100010;
char str[N]; //待输入的字符串
int son[N][26],//存储N节点的子节点,只会有26中情况(26个字母)
    cnt[N],//以N节点结尾的字符串的数量
    idx;//当前正在操作的节点的下标.下标是0的点既是根节点又是空节点

 void insert(char str[]){//存储操作
     int p = 0;
     for (int i = 0; str[i]; ++i) {//直到str都存进去了,字符串结尾是/0
         int u = str[i]-'a';//当前字母子节点编号,a~z=>0~25
         if(!son[p][u]) son[p][u]=++idx;
         p=son[p][u];
     }
     //循环结束时,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() {
//    std::cout << "Hello, World!" << std::endl;
    int n;
    scanf("%d",&n);
    while(n--){
        char op[2];//操作类型
        scanf("%s%s",op,str);
        if(op[0]=='I') insert(str);
        else printf("%d\n", query(str));
    }
    return 0;
}

4. 例题2: 最大异或对

你可能感兴趣的:(算法,算法,c++)