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

这是用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. 十三、栅栏CyclicBarrier

    一.简介 栅栏CyclicBarrier的作用就是等待一组线程都准备好了,然后执行某个任务.这与CountDownLatch很相似. 但是CyclicBarrier和CountDownLatch是有区 ...

  2. memcached 细究(一)

    memcached是高性能的分布式的内存缓存服务器.由国外社区网站LIVEJOURNAL的开发团队开发. 使用目的: 通过缓存数据库查询结果,减少数据库的访问次数,以提高动态web应用的速度.提高可扩 ...

  3. Django之ModelForm使用

    一:什么是ModelForm呢? Model + Form ==> ModelForm.model和form的结合体,所以有以下功能: 数据验证 数据库操作 model有操作数据库的字段,for ...

  4. Java类中的各种成员的加载顺序

    //执行顺序:(优先级从高到低.)静态代码块>mian方法>构造代码块>构造方法. 其中静态代码块只执行一次.构造代码块在每次创建对象是都会执行. 1 普通代码块 1 //普通代码块 ...

  5. IhyerDB modBus采集器配置.

    近期查了一下ihyerDB-modbus采集器的相关配置,由于没有相关的modbus设备,于是今天上午根据网上的线索下载了Modbus Slave(modbus从站仿真器).笔记本也没有串口,于是下载 ...

  6. SpringBoot整合mybatis——配置mybatis驼峰命名规则自动转换

    一.简述 mybatis驼峰式命名规则自动转换: 使用前提:数据库表设计按照规范“字段名中各单词使用下划线"_"划分”: 使用好处:省去mapper.xml文件中繁琐编写表字段列表 ...

  7. C#代码处理网页关于登录的code

    作者:血饮狂龙链接:https://www.zhihu.com/question/49452639/answer/117294801来源:知乎著作权归作者所有,转载请联系作者获得授权. private ...

  8. 小程序填坑之路(二):cover-view

    https://blog.csdn.net/mr_chen95/article/details/79261987 在一些情况下,我们需要对map.video.canvas.camera这些微信小程序的 ...

  9. Linux常用命令(随时补充)

    1.系统 1.1.系统关闭.重启 1)关闭:shutdown -h now 2)重启:reboot.init 6 1.2.修改默认网卡 1)vi /etc/udev/rules.d/70-persis ...

  10. select 1 from table 语句中的1代表什么意思

    在这里我主要讨论的有以下几个select 语句: doo_archive表是一个数据表,表的行数为4行,如下: 分别用三条select语句select  1 from doo_archive.sele ...