hdu 1251(字典树)
来源:互联网 发布:时间序列数据下载 编辑:程序博客网 时间:2024/05/16 09:34
经典的字典树题目。。字典树:又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种。典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计。它的优点是:利用字符串的公共前缀来节约存储空间,最大限度地减少无谓的字符串比较,查询效率比哈希表高。#include <iostream>#include <map>#include <cstring>#include <cstdio>#include <string>#include <malloc.h>using namespace std;const int MAX=26;struct Trie{ Trie *next[MAX];//子字典树 int v; //根据需要变化};Trie *root;//根节点void createTrie(char *str){ int len = strlen(str); Trie *p = root, *q; if(len==0)return ; for(int i=0; i<len; i++) { int id = str[i]-'a'; if(p->next[id] == NULL)//未出现的节点 { q = (Trie *)malloc(sizeof(Trie)); for(int j=0; j<MAX; ++j)//建p的子树 q->next[j] = NULL; p->next[id] = q; p=q; p->v = 1;//初始化 } else//已经出现 { p = p->next[id]; p->v++; } } return ;}int findTrie(char *str){ int len = strlen(str); if(len==0)return 0; Trie *p = root; for(int i=0; i<len; ++i) { int id = str[i]-'a'; if(p->next[id]==NULL ) //若为空集,表示不存以此为前缀的串 return 0; else p=p->next[id]; } return p->v; //此串是字符集中某串的前缀}int main(){ //根的初始化 root =( Trie *)malloc(sizeof( Trie)); for(int i=0;i<MAX;i++) { root->next[i]=NULL; } root->v =1; char s[300]; while(gets(s)&&s[0]!='\0') { createTrie(s);//插入 } while(scanf("%s",s)!=EOF) { cout<<findTrie(s)<<endl;//查找 } return 0;}
- hdu 1251(字典树)
- hdu 1251 字典树
- HDU 1251 字典树
- hdu 1251 字典树
- hdu 1251 字典树
- HDU-1251,字典树
- HDU 1251 字典树
- 1251 hdu 字典树
- HDU 1251(字典树)
- HDU 1251 字典树
- HDU 1251(字典树)
- HDU 1251 字典树
- hdu 1251 字典树
- HDU 1251 字典树
- 字典树 hdu 1251
- hdu 1251 字典树
- HDU 1251 字典树
- hdu 1251 字典树
- c++第八周【任务3】实现分数类中的运算符重载,在分数类中可以完成分数的加减乘除(运算后再化简)、求反、比较(6种关系)的运算。
- OpenRTMFP/Cumulus Primer(3)图解CumulusEdge原理
- 共用体类型,枚举类型的使用
- 基于fiddler来模拟限速
- 开源IaaS软件的比较 — 构架、功能、社区、商业及其他
- hdu 1251(字典树)
- 解决Fiddler查看Post参数中文乱码的问题
- 创业笔记(四)
- [POJ]1004 Financial Management
- poj 1873 The Fortified Forest(凸包+枚举)
- C语言运算符优先级
- ViewSwitcher实现程序列表分屏和动画效果
- 一些分布式系统体系结构名词介绍
- 分布式计算开源框架Hadoop介绍