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
;
}

 

最新文章

  1. XLT格式化XML那点事(C#代码中的问题解决)(二)
  2. (转)HTTP长连接和短连接
  3. Linux下C程序的编辑,编译和运行以及调试
  4. android6.0源码分析之Camera API2.0下的Capture流程分析
  5. Swift - 03 - 整数类型
  6. HDU 2639 Bone Collector II(01背包变型)
  7. 08-C语言循环
  8. liunx常用命令
  9. 测试工具——JMeter
  10. DBUtils源码分析
  11. Unity进阶----AssetBundle_02(加载依赖关系及网络资源)(2018/10/31)
  12. CDQ分治与整体二分学习笔记
  13. error C4996: &#39;sprintf&#39;: This function or variable may be unsafe.
  14. svn: 提交终止
  15. Lucene6.6.0 案例与学习路线
  16. 非[无]root权限 服务器 下安装perl以及perl模块
  17. 『Re』知识工程作业_主体识别
  18. mysql 按照 where in 排序
  19. koa中接收前台传递的各种数据类型的方式
  20. IDEA2018 license

热门文章

  1. java.io.IOException: Illegal partition for 67 (-1)
  2. CAP理论学习
  3. 今天学习Ibatis,花了我一个下午的时间,程序猿呀,你上点心吧
  4. 【JMeter4.0学习(八)】之断言
  5. div+css 画三角形
  6. Esper 20章 优化
  7. Vue实现组件props双向绑定解决方案
  8. Java常用代码工具类相关
  9. iOS 蓝牙功能 bluetooth
  10. 第三方-Swift2.0后Alamofire的使用方法