码迷,mamicode.com
首页 > 其他好文 > 详细

字典树模板

时间:2017-04-01 09:24:57      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:==   struct   root   current   roo   turn   for   bsp   span   

const int N=27;

struct node{
    int cnt;
    node* childs[N];
    node(){
        cnt=0;
        for(int i=0;i<N;i++)
            childs[i]=NULL;
    }
};

node *root=new node();
node *current,*newnode;

void insert(char *str){
    current=root;
    for(int i=0;i<strlen(str);i++){
        int m=str[i]-a;
        if(current->childs[m]!=NULL){
            current=current->childs[m];
            ++(current->cnt);
        }else{
            newnode=new node;
            ++(newnode->cnt);
            current->childs[m]=newnode;
            current=newnode;
        }
    }
}

int search(char *str){
    current=root;
    for(int i=0;i<strlen(str);i++){
        int m=str[i]-a;
        if(current->childs[m]==NULL)
            return 0;
        current=current->childs[m];
    }
    return current->cnt;
}

 

字典树模板

标签:==   struct   root   current   roo   turn   for   bsp   span   

原文地址:http://www.cnblogs.com/IKnowYou0/p/6654480.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!