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

这是用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. C# 相对路径指定详解

    1. 根目录 .\\ 或者直接给出文件名称,是找根目录的路径. 如:path = "gs.mdb" 与 path = ".\\gs.mdb"是一个意思. 2. ...

  2. 【转载】2012年七个免费ASP空间分享-支持ASP、ASP.NET的空间

    文章目录 Azure空间 Appharbor 7host空间 Brinkster Jabry空间 总结后的话 这篇免费ASP空间的总结文章本来标题已经拟好了是:2012年十大免费ASP空间分享,但是当 ...

  3. [linux] shell脚本编程-ubuntu创建vsftpd服务

    1.  useradd -s /bin/bash -m 用户名 ,  创建用户,自动创建家目录 , 设置登录shell 2.  echo 用户名:密码 | chpasswd ,非交互式设置密码 3.  ...

  4. 安装vmware player

    一.简介 什么是虚拟机? 虚拟机是通过软件来模拟一个完整的计算机系统.简单来说,你可以在当前系统中通过虚拟机软件运行另外一个系统,并且与当前系统隔离. 什么是vmware? vmware(virtua ...

  5. 工作经验:Java 系统记录调用日志,并且记录错误堆栈

    前言:现在有一个系统,主要是为了给其他系统提供数据查询接口的,这个系统上线不会轻易更新,更不会跟随业务系统的更新而更新(这也是有一个数据查询接口系统的原因,解耦).这时,这个系统就需要有一定的方便的线 ...

  6. Java:反射与代理

    Java世界的繁荣反射这一特性有很大功劳,可以获取全面的类型信息. /** * */ package ref; import java.lang.reflect.Field; import java. ...

  7. 关于iFrame特性总计和iFrame跨域解决办法

    1.iframe 定义和用法 iframe 元素会创建包含另外一个文档的内联框架(即行内框架). HTML 与 XHTML 之间的差异 在 HTML 4.1 Strict DTD 和 XHTML 1. ...

  8. vue-cli构建项目 npm run build后应该怎么运行在本地查看效果

    问题: 就是 bulid 打包后,想本地看看效果,本地看不了.... 网上看到一个....   具体更多在: http://www.dabaipm.cn/static/frontend/346.htm ...

  9. EF6 按条件更新多行记录的值

    using (var db = new MyDbContext()) { string fromUser = ""; //sender string toUser = " ...

  10. 结合java的反射和泛型性质简化JDBC和相应的同步等服务器数据库操作代码

    github地址:https://github.com/hzphzp/HeartTrace_Server 我们的服务器端数据库并没有用sqllite, 而是直接用mysql,并且用JDBC直接进行操作 ...