trie模板

来源:互联网 发布:cctv直播软件下载 编辑:程序博客网 时间:2024/06/06 02:12
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] );    }};

0 0
原创粉丝点击