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] );
}
};
trie模板
最新推荐文章于 2022-03-20 21:37:28 发布