trie树(字典树)实现 C++
来源:互联网 发布:znds网络电视论坛官网 编辑:程序博客网 时间:2024/06/06 02:45
#include <iostream>#include <vector>#include <string>#include <map>using namespace std;struct TrieNode{ vector<TrieNode*> next; bool end; int cnt; TrieNode():end(0),cnt(0){}};TrieNode *root=new TrieNode();map<string,unsigned int> table;void build(string &str){ TrieNode *p=root; for(unsigned int x:str) { if(x+1>p->next.size()) p->next.resize(x+1); if(p->next[x]==NULL) { TrieNode* s=new TrieNode(); p->next[x]=s; } p=p->next[x]; } p->end=true; p->cnt++;}void searchAllword(TrieNode *p,string word){ if(p->end==true) table[word]=p->cnt; for(unsigned int i=0;i<p->next.size();++i) { TrieNode *xp=p->next[i]; if(xp) searchAllword(xp,word+(char)i); }}int search(string &str){ TrieNode *p=root; for(unsigned int x:str) { if(x>=p->next.size()||p->next[x]==NULL) return 0; else p=p->next[x]; } return p->cnt;}void disp(){ for(auto x:table) cout<<x.first<<" "<<x.second<<endl;}int main(int argc, char const *argv[]){ int n; string str; cout<<"input word number that you want to creat: "; cin>>n; while(n--) { cin>>str; build(str); } cout<<"input word number that you want to search: "; cin>>n; while(n--) { cin>>str; cout<<str<<" : "<<search(str)<<endl; } searchAllword(root,string()); cout<<"all word record\n"; disp(); return 0;}
阅读全文
0 0
- 字典树(Trie树)的C程序实现代码
- trie字典树实现
- 7. implement-trie(实现trie字典树)
- PHP实现Trie树(字典树)
- 字典树(Trie树) C++实现
- trie树(字典树)实现 C++
- Trie树(字典树)的实现
- Trie树(字典树)的实现
- 字典树(Trie)的java实现
- Trie(字典树)的Java实现
- 字典树(Trie)
- Trie(字典)树
- 字典树(Trie)
- 字典树(trie)
- 字典树(Trie)
- 字典树(trie)
- Trie(字典树)
- Trie(字典树)
- JVM自动内存管理机制
- android中代码解析drawable的xml文件
- H.264 NALU语法结构——写的最好的一篇,非常详细
- 锚点跳转滑动效果
- BZOJ3626LCA(树剖+线段树+LCA+差分)
- trie树(字典树)实现 C++
- [JSOI2008]最大数(栈+二分||动态加点的线段树)
- MyBatis解决Java实体类和表字段不一致方法
- strcmp函数的实现
- 作业二十六 Arbitrage
- 困难的串uva129(dfs)
- Java interface中方法和Object方法冲突
- 如何将多个excel文件合并成一个
- 友盟多渠道打包