字典树

字典树

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

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];
    }
};
posted @ 2020-07-21 11:45  caoanda  阅读(114)  评论(0编辑  收藏  举报