trie模板

struct trie{
    int tot,root;
    int child[maxnode][charset];
    bool flag[maxnode];
    trie(){
        memset ( child[1], 0, sizeof ( child[1] ) );
        flag[1] = false;
        root=tot=1;
    }
    void insert( const char* str ){
        int *cur = &root;
        for ( const char *p = str; *p; *p ++ ) {
            cur = &child[*cur][*p-base];
            if ( *cur == 0 ){
                *cur = ++ tot;
                memset ( child[tot], 0, sizeof( child[tot] ) );
                flag[tot] = false;
            }
        }
        flag[*cur] = true;
    }
    bool query ( const char *str ){
        int *cur = &root;
        for ( const char *p = str; *p && *cur; ++p ){
            cur = &child[*cur][*p-base];
        }
        return ( *cur && flag[*cur] );
    }
};

你可能感兴趣的:(模板)