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

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

  1. #include <iostream>
  2. #include <malloc.h>
  3. #include <string>
  4. using namespace std;
  5.  
  6. typedef struct node{
  7. int pass;
  8. ];
  9. }
  10. *trieTree;
  11.  
  12. trieTree init() {
  13. trieTree t = (trieTree)malloc(sizeof(node));
  14. ; i < ; i++)t->next[i] = NULL;
  15. t->pass = ;
  16. return t;
  17. }
  18.  
  19. void insert(trieTree T,string s) {
  20. node *n = T;
  21. ; i < s.length(); i++) {
  22. int index = s[i] - 'a';
  23. if (T->next[index] == NULL) {
  24. node *t = init();
  25. T->next[index] = t;
  26. }
  27. T = T->next[index];
  28. T->pass++;
  29. }
  30. }
  31. int find(trieTree T, string s) {
  32. node *n = T;
  33. ; i < s.length(); i++) {
  34. int index = s[i] - 'a';
  35. if (T->next[index] == NULL) {
  36. return NULL;
  37. }
  38. T = T->next[index];
  39. }
  40. return T->pass;
  41. }
  42. int main() {
  43. trieTree T = init();
  44. string s;
  45. while (getline(cin,s)) {
  46. if (s.empty()) break;
  47. insert(T, s);
  48. }
  49.  
  50. while (getline(cin,s)) {
  51. cout << find(T, s) << endl;
  52. }
  53. ;
  54. }

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

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

  1. #include <iostream>
  2. #include <map>
  3. #include <string>
  4. using namespace std;
  5.  
  6. typedef struct node{
  7. int pass;
  8. map<char,struct node *>m;
  9. }
  10. *trieTree;
  11.  
  12. trieTree init() {
  13. trieTree t = new node;
  14. t->pass = ;
  15. return t;
  16. }
  17.  
  18. void insert(trieTree T,string s) {
  19. ; i < s.length(); i++) {
  20. if (T->m.find(s[i]) == T->m.end()) {
  21. node *t = init();
  22. T->m.insert(make_pair(s[i], t));
  23. }
  24. T = T->m[s[i]];
  25. T->pass++;
  26. }
  27. }
  28. int find(trieTree T, string s) {
  29. node *n = T;
  30. ; i < s.length(); i++) {
  31. if (T->m.find(s[i]) == T->m.end()) {
  32. return NULL;
  33. }
  34. T = T->m[s[i]];
  35. }
  36. return T->pass;
  37. }
  38. int main() {
  39. trieTree T = init();
  40. string s;
  41. while (getline(cin,s)) {
  42. if (s.empty()) break;
  43. insert(T, s);
  44. }
  45.  
  46. while (getline(cin,s)) {
  47. cout << find(T, s) << endl;
  48. }
  49. ;
  50. }

随手练——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. 删除弹出提示框_MVC

    <td> @Ajax.ActionLink(@shared.Delete, "DeleteServicetag", new { id = item.ID }, new ...

  2. 云主机安装Tomcat上传自己的网站

    前几天在DigitalOcean上买一个云服务器(1g内存,1核,25gssd,1tb流量,一个月5$,按天收费),用github的students developer package里面的优惠码拿到 ...

  3. jQuery选择器,外加例子讲解

    jQuery选择器的优势: (1) 代码更简单(2) 支持CSS1到CSS3选择器(3) 完善的处理机制 jQuery选择器的分类 (1) 基本选择器 基本选择器是jQuery中使用最多的选择器,它又 ...

  4. [JAVA IDEA]在使用maven项目中,无法读取resources文件夹中的配置文件的一种解决方案

    1.在通过配置文件来连接数据库时,在resouces文件中放入了db.properties配置文件,但无法正常读取到 读取配置文件信息的代码: InputStream input=JdbcUtil.c ...

  5. HTTP 错误500.19 - 错误代码 0x80070021

    1.错误描述 HTTP 错误500.19 -Internal Server Error 无法访问请求的页面,因为该页的相关配置数据无效. 详细错误信息 模块 IIS Web Core  通知 Begi ...

  6. javascript之url转义escape()、encodeURI()和decodeURI(),ifram父子传参参数有中文时出现乱码

    ifram父子传参参数有中文时出现乱码,可先在父级页面用encodeURI转义,在到子页面用进行decodeURI()解码 我们可以知道:escape()除了 ASCII 字母.数字和特定的符号外,对 ...

  7. 第二天-while循环 格式化输出 运算符 编码

    一.while循环 while 条件: 语句块(循环体)     #判断条件是否成立,若成立执行循环体,然后再次判断条件...直到不满足跳出循环 else: 当条件不成立的时候执行这里,和break没 ...

  8. SignalR实时通信

    /// <summary> /// Message集线器类 /// </summary> [HubName("chathub")] public class ...

  9. C++学习笔记(6)----基类和派生类的构造函数和析构函数的执行顺序

    基类和派生类:构造函数和析构函数的执行顺序 在Visual Studio中,新建控制台工程,构造类如下: #include<iostream> using namespace std; c ...

  10. IDEA 的缓存问题

    当IDEA还是使用以前的配置时,大概率是缓存问题,查看target,里面的内容就是编译好的东西,问题都是出自这里.