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. C----------输入一组整数,求出这组数字子序列和中的最大值,只要求出最大子序列的和,不必求出最大值对应的序列。

    © 版权声明:本文为博主原创文章,转载请注明出处 代码: #include <stdio.h> #include <stdlib.h> #define GET_ARRAY_LE ...

  2. swift - 全屏pop手势

    UINavigationController系统自带有侧滑手势,但是这个手势第一点只能边缘侧滑才可以有效,第二点当手动隐藏系统的导航时,这个手势就不能生效了 为了能到达到全屏pop的效果这里有2中解决 ...

  3. OpenOffice/LibreOffice的行距问题

    OpenOffice和LibreOffice的默认行距(行间距)都很宽,可以通过以下方法设置. 格式 -> 页面 -> 文字网格 -> 不使用网格

  4. codeforces 427 div.2 F. Roads in the Kingdom

    F. Roads in the Kingdom time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  5. k8s部署dashborad

    环境: 两台虚拟机, 10.10.20.203 部署docker.etcd.flannel.kube-apiserver.kube-controller-manager.kube-scheduler ...

  6. 在express项目中使用redis

    在express项目中使用redis 准备工作 安装redis 安装redis桌面管理工具:Redis Desktop Manager 项目中安装redis:npm install redis 开始使 ...

  7. C语言基础知识【循环】

    C 循环1.有的时候,我们可能需要多次执行同一块代码.一般情况下,语句是按顺序执行的:函数中的第一个语句先执行,接着是第二个语句,依此类推.编程语言提供了更为复杂执行路径的多种控制结构.循环语句允许我 ...

  8. 将非递减有序排列(L L1)归并为一个新的线性表L2 线性表L2中的元素仍按值非递减

    #include "stdio.h"#include "stdlib.h"#include "function.h"void main(){ ...

  9. [转】IIS:Do not nest virtual directories

    原文:https://msdn.microsoft.com/en-us/library/ms178685.aspx#Anchor_6 Configuration settings for virtua ...

  10. Django使用富文本编辑器

    1.下载kindeditor 网址:http://kindeditor.net/demo.php2.解压到项目中 地址:\static\js\kindeditor-4.1.103.删除没用的文件 例如 ...