字典树(指针 找单词)

struct node
{
    node *next[28];
    bool is;
    node()
    {
        is=false;
        memset(next,0,sizeof(next));
    }
};
void insert(node *root,char s[])
{
    int i=0;
    node *p=root;
    while(s[i])
    {
        int j=s[i++]-'a';
        if(p->next[j]==NULL) p->next[j]=new node();
        p=p->next[j];
    }
    p->is=true;
}
int search(node *root,char s[])
{
    i=0,len=0;
    node *p=root;
    while(s[i])
    {
        int j=s[i++]-'a';
        if(p->next[j]==NULL) return 0;  //查不到
        p=p->next[j];
    }
    return 1;
}

猜你喜欢

转载自www.cnblogs.com/ww123/p/9095196.html