HDU1251统计难题---Trie Tree
来源:互联网 发布:linux echo 脚本 编辑:程序博客网 时间:2024/05/26 05:50
map巧过
#include <stdio.h>#include <string.h>#include <map>#include <string>using namespace std;map<string,int>m1;int main(){ char z[10]; while(gets(z)) { if(strlen(z)==0) break; for(int i=strlen(z);i>0;i--) { z[i]='\0'; m1[z]++; } } while(gets(z)) { printf("%d\n",m1[z]); } return 0;}
经典前缀树
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;struct node{ int Count;///以此节点为前缀的单词数 node *next[26];///26叉树 node() ///初始化数据 { for (int i=0;i<26;i++) next[i]=NULL; Count=0; }};node *p,*root=new node();void Insert(char *s)///插入新单词,即建立字典树{ int i,k; p=root; for (i=0; s[i]!='\0'; i++) { k=s[i]-'a'; if (p->next[k]==NULL) p->next[k]=new node(); ///判断是不是新节点,如果是分配创建一个新节点来存贮 , ///即root的next域对应的k位置是否为空 p=p->next[k];///向前走一步 p->Count++; ///以此位置为前缀的数量+1 }}int Search(char *s){ int i,k; p=root; for (i=0; s[i]!='\0'; i++) { k=s[i]-'a'; if (p->next[k]==NULL)///一旦查找不到,立即跳出 return 0; p=p->next[k]; } return p->Count;}int main(){ char s[11]; while (gets(s)) { int len=strlen(s); if (len==0) break; Insert(s); } while (gets(s)) { printf("%d\n",Search(s)); } return 0;}
第一个字典树(G++内存超限),第二个map(红黑树),对于此类问题,字典树效率优势明显
阅读全文
0 0
- HDU1251统计难题---Trie Tree
- hdu1251 统计难题 (Trie)
- hdu1251 统计难题 trie
- HDU1251 统计难题 【trie树】
- HDU1251 统计难题(Trie)
- HDU1251统计难题 trie树
- HDU1251 统计难题(Trie树)
- hdu1251 统计难题 数据结构之Trie树
- HDU1251(统计难题)---Trie树简单应用
- HDU1251 统计难题(Trie树)
- hdu1251 统计难题(Trie树入门题)
- hdu1251-> 统计难题(Trie树入门题)
- HDU1251 统计难题(trie树[重做])
- HDU1251 统计难题 【Trie 树入门】
- [复习][HDU1251]字典树(trie树)统计难题
- 统计难题 hdu1251
- HDU1251统计难题
- 统计难题 hdu1251
- [树] [HDU5830] Rikka with Subset II
- mac下载配置mongodb数据库
- 线段树大模板(区间更新,单点更新,查询区间最值等等)
- 设计模式—原型模式(六)
- IMWeb提升营Day7 | 训练题37:数字在排序数组中出现的次数
- HDU1251统计难题---Trie Tree
- LeetCode 36. Valid Sudoku
- opencv3_java 图像的方框滤波BoxFilter BoxFilter
- Java架构师之路:Java程序员必看的15本书的电子版下载地址
- leetcode【第十三周】颜色排序
- xml
- c注释风格转化到c++注释风格
- 数组实现的线性表的基本操作2[C语言]
- leetcode【第十四周】从排序列表中移除重复元素Ⅱ