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
原创粉丝点击