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

这是重写的,让我感觉到每一次的理解程度都在增加
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
using namespace std;
struct
node
{

    int
sum;
    node *next[];
    node()//初始化数据
    {
        memset(next, NULL, sizeof(next));
        sum=;
    }
};

node *head=new node();//用C++的new动态申请内存其实delete和new是一对儿哦
node *now=(node *)malloc(sizeof(node));//用C语言动态申请内存
void buildtiretree(char *s)//建立字典数
{
    node *p=new node();
    p=head;
    for
(int i=; s[i]; i++)
    {

        int
k=s[i]-'a';
        if
(p->next[k]==NULL)//如果p->next[k]为空
            p->next[k]=new node();//就动态申请一个内存
        now=p->next[k];
        now->sum++;
        p=now;
        /*也可以这样写,其实就是第一个,也就是head不存任何东西
        p=p->next[k];
        p->sum++;
        */
    }
}

int
query(char *s)//查询单词
{
    node *p=new node();
    p=head;
    //node *p=head;
    for(int i=; s[i]; i++)
    {

        int
k=s[i]-'a';
        if
(p->next[k]==NULL)
            return
;
        p=p->next[k];
    }

    return
p->sum;
}

void
Free(node *head)//释放内存
{
    int
i;
    if
(head==NULL)
        return
;
    for
(i=; i<; i++)
    {

        if
(head->next[i]!=NULL)
            Free(head->next[i]);
    }

    free(head);
    head=NULL;
}

int
main()
{

    char
s[];
    while
(gets(s), s[])
        buildtiretree(s);
    while
(cin >> s)
    printf("%d\n", query(s));
    Free(head);
    return
;
}
之前写的
#include<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;
struct
node
{

int
sum;
node *next[];
};

void
buildtrietree(node *head, char s[])
{

node *p=new node();
p=head;
for
(int i=; s[i]; i++)
{

int
k=s[i]-'a';
if
(p->next[k]==)
p->next[k]=new node();
p=p->next[k];
p->sum++;
}
}
int query(node *head, char s[])
{

node *p=new node();
p=head;
for
(int i=; s[i]; i++)
{

int
k=s[i]-'a';
if
(p->next[k]==)
return
;
p=p->next[k];
}

return
p->sum;
}

int
main()
{

char
s[];
node *head=new node();
while
(gets(s), s[])
buildtrietree(head, s);
while
(cin >> s)
printf("%d\n", query(head, s));
return
;
}

 

字典树 HDU 1251 统计难题的更多相关文章

  1. hdu 1251 统计难题(字典树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 统计难题 Time Limit: 4000/2000 MS (Java/Others)    M ...

  2. HDU 1251 统计难题 (Trie)

    pid=1251">统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/ ...

  3. hdu 1251 统计难题 (字典树入门题)

    /******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...

  4. HDU 1251 统计难题(字典树 裸题 链表做法)

    Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己 ...

  5. hdu 1251:统计难题(字典树,经典题)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  6. HDOJ/HDU 1251 统计难题(字典树啥的~Map水过)

    Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己 ...

  7. [ACM] hdu 1251 统计难题 (字典树)

    统计难题 Problem Description Ignatius近期遇到一个难题,老师交给他非常多单词(仅仅有小写字母组成,不会有反复的单词出现),如今老师要他统计出以某个字符串为前缀的单词数量(单 ...

  8. HDU 1251 统计难题(字典树模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意:给出一些单词,然后有多次询问,每次输出以该单词为前缀的单词的数量. 思路: 字典树入门题. #inc ...

  9. HDU 1251 统计难题(字典树入门模板题 很重要)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

随机推荐

  1. 设置VisualSVN在提交修改时必须输入一定数量的备注信息

    我发现在使用SVN中,提交时,很多人不习惯填写备注信息,虽然在培训中.平时使用时多次提醒备注信息的好处,但是效果不大,每次提交时还是不写,或者随便写两字. 所以很有必要通过系统设置强制填写足够数量的备 ...

  2. Atitit.软件硕士  博士课程 一览表 attilax 总结

    Atitit.软件硕士  博士课程 一览表 attilax 总结 1. Attilax聚焦的领域1 2. 研究生硕士博士课程汇总表1 3. 博士课程3 4. Attilax额外的4 5. 参考4 1. ...

  3. iOS UIView添加阴影

    _bottomView.layer.masksToBounds = NO; _bottomView.backgroundColor = [UIColor whiteColor]; _bottomVie ...

  4. Apatar 学习文档

    1.   Apatar数据集成简介 Apatar是一个开源跨平台数据集成工具,可以安装和运行在任何机器这有一些类型的用户界面.该工具用于启用批处理数据集成和提供简单的用户界面,这样任何人,不仅仅是技术 ...

  5. warning: push.default is unset; its implicit value is changing in Git 2.0 from 'matching' to 'simple'.

    'matching'参数是 git 1.x 的默认行为,其意是如果你执行 git push 但没有指定分支,它将 push 所有你本地的分支到远程仓库中对应匹配的分支. 而 Git 2.x 默认的是 ...

  6. 01 linux上安装 nginx

    一:linux上安装 nginx 下载nginx:wget http://nginx.org/download/nginx-1.6.2.tar.gz 解压:tar zxvf nginx-1.6.2.t ...

  7. 中国版Office 365混合部署功能

    中国版Office 365混合部署功能已经正式上线了(原计划6月份推出),虽然支持的类型不如国际版的Office 365全面,但这也标志了该功能与之前相比,已经迈出了重要一步.目前中国版Office ...

  8. chattr

    chattr 功能:设置文件隐藏属性常用参数:+    增加某个特殊权限,其他原本存在的参数不动-     删除某个特殊权限,其他原本存在的参数不动=    设置一定,且仅有后面接的参数 i   文件 ...

  9. 一篇很不错的关于WPF DataGrid的文章,包含validation

    https://www.codeproject.com/Articles/30905/WPF-DataGrid-Practical-Examples

  10. ios 推送 证书配置

    S的推送证书,有有效期限制,一般为一年.当我们证书过期的时候,就需要重新生成证书了.有一段时间没有上苹果网站了,昨天上去一看,此奥,改版了,下边我们将重新生成一个正式环境的push推送的证书. 1.先 ...