首页 > 其他 > 详细

HDU1251(字典树)

时间:2016-01-11 09:04:07      阅读:126      评论:0      收藏:0      [点我收藏+]
#include"cstdio"
#include"cstring"
using namespace std;
const int N=26;
struct node{
    int t;
    node* next[N];
    node()
    {
        t=0;
        for(int i=0;i<N;i++)    next[i]=NULL;
    }
};
node* root;

void insert(char *s)
{
    node* p=root;
    for(int i=0;s[i];i++)
    {
        int k=s[i]-a;
        if(p->next[k]==NULL)    p->next[k]=new node();
        p=p->next[k];
        p->t++;
    }
}

int search(char *s)
{
    node *p=root;
    int i;
    for(i=0;s[i];i++)
    {
        int k=s[i]-a;
        if(p->next[k]==NULL)    return 0;
        p=p->next[k];
    }
    return p->t;
}
void del(node* p)
{
    for(int i=0;i<N;i++)
    {
        if(p->next[i]!=NULL)
        {
            del(p->next[i]);
        }        
    }
    delete p;
}
int main()
{
    char s[15];
    root=new node();
    while(gets(s)&&*s)
    {
        insert(s);
    }
    while(gets(s))
    {
        printf("%d\n",search(s));
    }
    del(root);
    return 0;
}

 

HDU1251(字典树)

原文:http://www.cnblogs.com/program-ccc/p/5120110.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!