Trie模板
来源:互联网 发布:神泣单机数据库 编辑:程序博客网 时间:2024/06/05 17:57
struct Trie{ int ch[maxnode][sigma_size]; int sz; int cont[maxnode]; Trie() {memset(ch,0,sizeof ch);memset(cont,0,sizeof cont);sz=1;} int idx(char c) {return c-'a';} void insert(char *s) { int u=0,l=strlen(s); for(int i=0;i<l;++i){ int c=idx(s[i]); if(!ch[u][c]){ ch[u][c]=sz++; } u=ch[u][c]; cont[u]++; //字符串的最后一个节点记录信息 } } int query(char *s) { int u=0,l=strlen(s); for(int i=0;i<l;++i){ int c=idx(s[i]); if(!ch[u][c]) return 0; u=ch[u][c]; } return cont[u]; }}T;
#include<iostream>#include<cstring>#include<cstdio>#include<cstdlib>#define maxnode 10000#define sigma_size 26using namespace std;struct tree{ int num; struct tree* next[sigma_size];};struct tree *root;void insert(char *s){ struct tree *p=root,*q; int l=strlen(s); for(int i=0;i<l;++i){ int c=s[i]-'a'; if(p->next[c]==NULL){ q=(struct tree* )malloc(sizeof(struct tree)); q->num=1; for(int j=0;j<sigma_size;++j){ q->next[j]=NULL; } p->next[c]=q; p=p->next[c]; } else{ p=p->next[c]; p->num++; } }}int query(char *s){ struct tree *p=root; int l=strlen(s); for(int i=0 ;i<l;++i){ int c=s[i]-'a'; p=p->next[c]; if(p==NULL) return 0; } return p->num;}void deal(struct tree * p){ if(p==NULL) return; for(int i=0;i<sigma_size;++i){ if(p->next[i]!=NULL){ deal(p->next[i]); } } free(p); return ;}root=(struct tree* )malloc(sizeof(struct tree));for(int i=0;i<sigma_size;++i){ root->next[i]=NULL;}root->num=0;
0 0
- Trie模板
- Trie 模板
- Trie模板
- trie模板
- Trie模板
- Trie模板
- Trie模板
- Trie模板
- 【模板】Trie树模板
- trie树的模板
- trie树入门(模板)
- trie树模板
- 字典树(trie)模板
- Trie字典树【模板
- Trie树模板
- Trie树模板
- Trie树模板
- 字典树Trie 模板
- 【.NET】 DictionaryCloneable 字典类Dictionary复制
- 有向图的强连通分支及DAG
- CentOs service 管理
- UVA 10341 Solve It
- 高性能IO模式--Reactor和Proactor
- Trie模板
- document.body.scrollTop和document.documentElement.scrollTop在不同浏览器下的问题
- 大数相乘
- 仿酷狗音乐播放器开发日志十七——换肤功能的实现一
- 【leetcode系列】String to Integer (atoi)
- mysql 最基本的操作
- java基础-面试题
- HDU 4915 Parenthese sequence(瞎搞题)
- hdu 1085 Holding Bin-Laden Captive!