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

HDU1251 裸字典树

时间:2017-08-25 19:23:11      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:cin   iostream   ext   end   malloc   get   out   creat   find   

HDU1251:http://acm.hdu.edu.cn/showproblem.php?pid=1251

初学字典树,码模板……

#include<iostream>
#include<string.h>
using namespace std;
char a[15];
struct node {
    node *nextt[26];
    int v=0;
};
node root;
void init()
{
    for (int i = 0; i < 26; i++)
    {
        root.nextt[i] = NULL;
    }
}
void creatree()
{
    int len = strlen(a);
    node *p = &root,*q;
    for (int i = 0; i < len; i++)
    {
        int id = a[i] - a;
        if (p->nextt[id] == NULL)
        {
            q = (node*)malloc(sizeof(node));
            q->v = 1;
            for (int j = 0; j < 26; j++)
                q->nextt[j] = NULL;
            p->nextt[id] = q;
            p = p->nextt[id];
        }
        else {
            p->nextt[id]->v++;
            p = p->nextt[id];
        }
    }
}
int find()
{
    int len = strlen(a);
    node *p = &root;
    for (int i = 0; i < len; i++)
    {
        int id = a[i] - a;
        p = p->nextt[id];
        if (p == NULL) return 0;
    }
    return p->v;
}
int main()
{
    init();
    while (gets(a) && a[0] != \0)
        creatree();
    while (cin >> a)
    {
        cout << find() << endl;
    }
    return 0;
}

 

HDU1251 裸字典树

标签:cin   iostream   ext   end   malloc   get   out   creat   find   

原文地址:http://www.cnblogs.com/Egoist-/p/7429362.html

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