字典树
字典树
字典树比较普通字符串比较而言,字符的可操作性更强
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];
    }
};
    CAD加油!欢迎跟我一起讨论学习算法,QQ:1401650042

 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号