hdu 1251 统计难题 字典树
来源:互联网 发布:java ssh2 编辑:程序博客网 时间:2024/06/12 22:42
字典树,又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种。典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计。它的优点是:利用字符串的公共前缀来减少查询时间,最大限度地减少无谓的字符串比较,查询效率比哈希树高。
它有3个基本性质:
1.根节点不包含字符,除根节点外每一个节点都只包含一个字符;
2.从根节点到某一节点,路径上经过的字符连接起来,为该节点对应的字符串;
3.每个节点的所有子节点包含的字符都不相同。
模板题
#include<cstdio>#include<queue>#include<iostream>#include<vector>#include<map>#include<cstring>#include<string>#include<set>#include<stack>#include<algorithm>#define cle(a) memset(a,0,sizeof(a))#define inf(a) memset(a,ox3f,sizeof(a))#define ll long long#define Rep(i,a,n) for(int i=a;i<=n;i++)using namespace std;const int INF = ( 2e9 ) + 2;//const int maxn =struct node{ int t; node *Next[26]; node() { for(int i=0;i<26;i++) this->Next[i]=NULL; t=0; }};node *root=new node; // root 根结点不存数据 void insert(char *s){ int len=strlen(s); node *cur=root; for(int i=0;i<len;i++) { if(cur->Next[s[i]-'a']==NULL) cur->Next[s[i]-'a']=new node; cur=cur->Next[s[i]-'a']; cur->t++; }}int find(char *s){ node *cur=root; int len=strlen(s); int i=0; for(;i<len;i++) { if(cur->Next[s[i]-'a']!=NULL) cur=cur->Next[s[i]-'a']; else break; } if(i==len)return cur->t; else return 0;}int main(){ char s[20]; while(gets(s),strcmp(s,"")) { insert(s); } while(scanf("%s",s)!=EOF) printf("%d\n",find(s));}
阅读全文
0 0
- HDU 1251 统计难题 字典树
- HDU 1251统计难题(字典树)
- [字典树 ]hdu 1251 统计难题
- 统计难题 hdu 1251 字典树
- hdu 1251 统计难题 字典树
- HDU 1251 统计难题 -- 字典树
- HDU 1251 统计难题(字典树)
- hdu 1251 统计难题(字典树)
- hdu 1251 统计难题(字典树)
- hdu 1251 统计难题(字典树)
- hdu-1251-统计难题(字典树)
- hdu 1251 统计难题 (字典树)
- HDU 1251统计难题 字典树
- hdu 1251 统计难题 (字典树)
- HDU 1251 统计难题(字典树)
- hdu 1251 统计难题 字典树
- hdu 1251 统计难题 字典树
- hdu 1251 统计难题 (字典树)
- maven项目搭建dubbo的消费者和生产者
- C++字符串和整数相互转换
- 如何使用Python plt像MATLAB一样绘图
- python3 [爬虫入门实战]爬虫之scrapy爬取中华人民共和国民政部
- 447. Number of Boomerangs
- hdu 1251 统计难题 字典树
- 模板究竟为什么不支持分离编译呢?!!!
- 空指针异常
- spring_boot_pay支付宝,微信,银联支付详细代码案例
- Vijos 1097 合并果子
- linux常用命令
- IScroll那些事——内容不足时下拉刷新
- Python数据类型 之 int
- mbr