hihoCoder_1014
来源:互联网 发布:淘宝模板什么意思 编辑:程序博客网 时间:2024/05/17 22:39
#include <iostream> #include <string>using namespace std;//字典树节点定义typedef struct Trie{ Trie* child[26]; int num; Trie(){ for(auto &a:child) a=NULL; num=1; }}Trie;//字典树类的操作插入、查找class DicTree{public: DicTree(){ p=new Trie(); } void addWord(string s){ Trie *q=p; for(int i=0;i<s.size();i++){ int a=s[i]-'a'; if(q->child[a]==NULL){ q->child[a]=new Trie(); }else{ q->child[a]->num++; } q=q->child[a]; } } int searchWord(string s){ Trie *q=p; for(int i=0;i<s.size();i++){ int a=s[i]-'a'; if(q->child[a]==NULL) return 0; if(i==s.size()-1) return q->child[a]->num; q=q->child[a]; } return 0; }private: Trie *p; };int main(){ int n,m; DicTree dictree; cin>>n; while(n--){ string tmp; cin>>tmp; dictree.addWord(tmp); } cin>>m; while(m--){ string tmp; cin>>tmp; cout<<dictree.searchWord(tmp)<<endl; } return 0;}
0 0
- hihoCoder_1014
- hihocoder_1014: Trie树(Trie树模板题)
- android欢迎页通知栏隐藏
- 关于安卓回调的一些理解
- HDOJ 1201 18岁生日
- 浅析C/C++中的switch/case陷阱
- Codeforces Round #363 (Div. 1) C LRU
- hihoCoder_1014
- hdoj 4548 美素数
- 设计模式之代理模式
- Scalaz(49)- scalaz-stream: 深入了解-Sink/Channel
- android沉浸式
- jQuery Mobile 工具栏
- poj Aggressive cows
- HDOJ 4548 美素数
- C学习笔记(十二)函数详解