首页 > 其他 > 详细

字典树

时间:2020-07-21 15:59:26      阅读:86      评论:0      收藏:0      [点我收藏+]

字典树

字典树比较普通字符串比较而言,字符的可操作性更强

const int maxn=5e5+5;		//maxn为总结点个数,不是总深度
struct trie{
    int nex[maxn][26],cnt=0;
    bool exist[maxn];
    void insert(string s){
        int slen=s.length(),p=0;
        for(int i=0;i<slen;++i){
            int c=s[i]-‘a‘;
            if(!nex[p][c]) nex[p][c]=++cnt;
            p=nex[p][c];
        }
        exist[p]=1;
    }
    bool find(string s){
        int slen=s.length(),p=0;
        for(int i=0;i<slen;++i){
            int c=s[i]-‘a‘;
            if(!nex[p][c]) return 0;
            p=nex[p][c];
        }
        return exist[p];
    }
};

字典树

原文:https://www.cnblogs.com/CADCADCAD/p/13354224.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!