其他分享
首页 > 其他分享> > 字典树模板

字典树模板

作者:互联网

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<string>
#include<set>
#include<algorithm>
using namespace std;
const int N = 26, M = 1000000;
int tot = 0;
struct Trie
{
    int next[N];
    bool val;
    int sum;
    void init()
    {
        memset(next, -1, sizeof(next));
        val = false;
    }
}Tire[M];
//int c[1000000],maxx=0;
int sum = 0;
void insert(string str)
{
    int len = str.length();
    int root = 0;
    for (int i = 0; i < len; i++)
    {
        int x = str[i] - 'a';
        if (Tire[root].next[x] == -1)
        {
            Tire[root].next[x] = ++tot;
            root = Tire[root].next[x];
            Tire[root].init();
        }
        else
        {
            root = Tire[root].next[x];
        }
        //Tire[root].val = false;
    }
        Tire[root].val = true;
}
bool search(string str)
{
    int len = str.length(), root = 0;
    for (int i = 0; i < len; i++)
    {
        int x = str[i] - 'a';
        /*if (Tire[root].val == true)
        {
            return true;
        }*/
        if (Tire[root].next[x] == -1)
        {
            return false;
        }
        else
        {
            root = Tire[root].next[x];
        }
    }
        return Tire[root].val;

    //return true;
}

 

标签:val,Tire,int,next,include,root,模板,字典
来源: https://blog.csdn.net/bobobo666/article/details/96430123