静态字典树和动态字典树模板
2014-08-01 20:24
225 查看
以前做字典树一直用动态的字典树,每次开辟新空间,需要 new ,删除时delete, 这样动态,节省内存,却要每次开辟和释放空间,饿很难浪费时间,
静态字典树, 是事先开辟一块足够的空间,需要时区取,计较节省时间,有时候却浪费了不必要的内存,写静态字典树,可以开二维数组,也可以用结构体加上指针;
静态字典树, 是事先开辟一块足够的空间,需要时区取,计较节省时间,有时候却浪费了不必要的内存,写静态字典树,可以开二维数组,也可以用结构体加上指针;
结构体 静态字典树模板
#define MAX 26 //字符集大小 typedef struct TrieNode { int nCount; //记录该字符出现次数 struct TrieNode *next[MAX]; }TrieNode; TrieNode Memory[1000000]; int allocp = 0; /*初始化*/ void InitTrieRoot(TrieNode **pRoot) { *pRoot = NULL; } /*创建新结点*/ TrieNode *CreateTrieNode() { int i; TrieNode *p; p = &Memory[allocp++]; p->nCount = 1; for(i = 0 ; i < MAX ; i++) { p->next[i] = NULL; } return p; } /*插入*/ void InsertTrie(TrieNode **pRoot , char *s) { int i , k; TrieNode *p; if(!(p = *pRoot)) { p = *pRoot = CreateTrieNode(); } i = 0; while(s[i]) { k = s[i++] - 'a'; //确定branch if(p->next[k]) p->next[k]->nCount++; else p->next[k] = CreateTrieNode(); p = p->next[k]; } } //查找 int SearchTrie(TrieNode **pRoot , char *s) { TrieNode *p; int i , k; if(!(p = *pRoot)) { return 0; } i = 0; while(s[i]) { k = s[i++] - 'a'; if(p->next[k] == NULL) return 0; p = p->next[k]; } return p->nCount; }
动态字典树模板
//专题地址http://vjudge.net/vjudge/contest/view.action?cid=51317#overview 此为动态字典树,静态字典树可自学。 #include <iostream> #include <cstdio> #include <string.h> using namespace std; typedef struct DT { DT *chid[52]; int end; DT() { end=0; memset(chid,0,sizeof(chid)); } }dt,*pdt; pdt root;//定义根节点 int getnum(char s) //根据字符得到num { if(s>='A'&&s<='Z')return s-'A'; else return s-'a'+26; } void insert(char *s) //插入字符串到字典树 { pdt next=root; int num; for(int i=0;s[i];i++) { num=getnum(s[i]); if(next->chid[num]==NULL) //为空则申请内存 next->chid[num]=new DT(); //开辟内存 next=next->chid[num]; } next->end=1; //字符串最末尾end赋值true return ; } int query(char *s) //查询字符串在字典树 { pdt next=root; int num; for(int i=0;s[i];i++) { num=getnum(s[i]); if(next->chid[num]==NULL) //没有此地址,返回未找到 return 0; next=next->chid[num]; //继续查找 } if(next->end) return 1; //找到返回true return 0; } void del(dt *rot) //递归删除字典树 { for(int i=0;i<52;i++) if(rot->chid[i]!=NULL) del(rot->chid[i]); delete rot; } char str[50002][50]; int main() { root=new DT(); //根节点分配内存 char a[]={"nihaoa"}; insert(a); printf("%d %d\n",query("nihaoa"),query("wobuhao")); //nihaoa存在 返回1 wobuhao不存在 返回0 del(root); //删除字典树 return 0; }
相关文章推荐
- 静态字典树和动态字典树模板
- hdu2846(动态字典树||静态字典树 模板)Repository
- HDU:1251-统计难题(字典树模板,动态建树,静态建树)
- 字典树模板(动态)
- GridView 动态添加模板列并绑定数据 实现从外部直接传入控件 和 绑定字段参数
- html+js+PHP(使用了smarty模板技术)+mysql实现二级动态下拉列表(select)
- Hdu 1251-统计难题(字典树模板题)
- 字典树模板/ac自动机模板
- 字典树模板题&hdu1251
- 字典树模板
- 字典树模板
- 编写GO的WEB开发框架 (十二): 配置及模板的动态重载
- 字典树(模板题目)
- 微信小程序编写tabBar模板,map组件markers属性动态初始化
- VC++ DLL的创建与动态调用模板
- 【android动态布局】之【ListView动态加载数据模板(使用xml布局)】
- .net c# 一个简单但是功能强大动态模板引擎
- ASP.NET 2.0 中动态添加 GridView 模板列的例子
- 如何动态添加模板列,且模板列中动态写入lable的ID?
- DataGrid动态添加模板列的一个例子