知识点:前缀树。典型的前缀树模板。

这是用next[26]数组的版本,超内存了。(后来发现,用C++交不会超,G++就会超)

#include <iostream>
#include <malloc.h>
#include <string>
using namespace std;

typedef struct node{
    int pass;
    ];
}
*trieTree;

trieTree init() {
    trieTree t = (trieTree)malloc(sizeof(node));
    ; i < ; i++)t->next[i] = NULL;
    t->pass = ;
    return t;
}

void insert(trieTree T,string s) {
    node *n = T;
    ; i < s.length(); i++) {
        int index = s[i] - 'a';
        if (T->next[index] == NULL) {
            node *t = init();
            T->next[index] = t;
        }
        T = T->next[index];
        T->pass++;
    }
}
int find(trieTree T, string s) {
    node *n = T;
    ; i < s.length(); i++) {
        int index = s[i] - 'a';
        if (T->next[index] == NULL) {
            return NULL;
        }
        T = T->next[index];
    }
    return T->pass;
}
int main() {
    trieTree T = init();
    string s;
    while (getline(cin,s)) {
        if (s.empty()) break;
        insert(T, s);
    }

    while (getline(cin,s)) {
        cout << find(T, s) << endl;
    }
    ;
}

一开始过不了,我还想了半天,网上别人代码,也是next[26]的写法都能AC,我咋过不了???人丑就不给过???这个难受啊。

其实,next指针数组,浪费了很多空间,用STL map重新改了一下(malloc只分配内存,我改成了new),内存大约节省了25%~30(自己实现一个简单键值对,节省的空间会更多),C++、G++编译器都能AC了。

#include <iostream>
#include <map>
#include <string>
using namespace std;

typedef struct node{
    int pass;
    map<char,struct node *>m;
}
*trieTree;

trieTree init() {
    trieTree t = new node;
    t->pass = ;
    return t;
}

void insert(trieTree T,string s) {
    ; i < s.length(); i++) {
        if (T->m.find(s[i]) == T->m.end()) {
            node *t = init();
            T->m.insert(make_pair(s[i], t));
        }
        T = T->m[s[i]];
        T->pass++;
    }
}
int find(trieTree T, string s) {
    node *n = T;
    ; i < s.length(); i++) {
        if (T->m.find(s[i]) == T->m.end()) {
            return NULL;
        }
        T = T->m[s[i]];
    }
    return T->pass;
}
int main() {
    trieTree T = init();
    string s;
    while (getline(cin,s)) {
        if (s.empty()) break;
        insert(T, s);
    }

    while (getline(cin,s)) {
        cout << find(T, s) << endl;
    }
    ;
}

随手练——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 统计难题(Trie模版题)

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

  5. HDU 1251统计难题

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

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

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

  7. hdu 1251 统计难题 trie入门

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

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

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

  9. HDU 1251 统计难题 (字符串-Trie树)

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

随机推荐

  1. JavaScript数组的三种定义方法

    数组的定义: <script type="text/javascript"> // <!--声明数组--> // 1.先声明数组长度,后进行赋值 var a ...

  2. 教你用go freetype根据用户昵称生成头像

    最近需要为用户服务添加一些新功能,其中就包括在注册时根据用户昵称生成头像这一点. 由于用户服务是用golang写的,google来google去都只找到freetype一个比较简单好用的库,其他比如I ...

  3. JS获取当前屏幕宽高

    Javascript: 网页可见区域宽: document.body.clientWidth网页可见区域高: document.body.clientHeight网页可见区域宽: document.b ...

  4. vmware创建centos虚拟机

    下载centos 安装之前你需要下载centos镜像:http://mirrors.aliyun.com/ 创建虚拟机 如果还没有安装vmware请参考:https://www.cnblogs.com ...

  5. JAVA基础之——方法直接用类名.的理解

    前言 在java中经常遇到使用classname.method()的方式调用方法,哪些场景需要用到呢,如下: 某些操作不依赖具体实例 某个方法是用频率较高,或者方法本身通用性较强,无需初始化类成员变量 ...

  6. gRPC的简单Go例子

    gRPC是一个高性能.通用的开源RPC框架,其由Google主要面向移动应用开发并基于HTTP/2协议标准而设计,基于ProtoBuf(Protocol Buffers)序列化协议开发,且支持众多开发 ...

  7. 关于容器、虚拟机以及 Docker 的一个入门教程

    Yves yao · 2017-09-05翻译 · 1315阅读 原文链接 huangxiaolu审校   源地址:http://zcfy.cc/article/a-beginner-friendly ...

  8. Zookeeper Curator API 使用

    0. 原生 ZOOKEEPER JAVA API  http://www.cnblogs.com/rocky-fang/p/9030438.html 1. 概述 Curator采用cache封装对事件 ...

  9. 常用SEO优化

  10. void()表达式结果是SyntaxError

    void是一元运算符,他出现在操作数之前,操作数可以使任意类型,操作数会照常计算,但忽略计算结果并返回undefined. 因此在操作数具有副作用的时候使用void来让程序根据语义 console.l ...