Trie树 [Hihocoder]
来源:互联网 发布:mysql数据库视频教程 编辑:程序博客网 时间:2024/05/22 12:32
输入
输入的第一行为一个正整数n,表示词典的大小,其后n行,每一行一个单词(不保证是英文单词,也有可能是火星文单词哦),单词由不超过10个的小写英文字母组成,可能存在相同的单词,此时应将其视作不同的单词。接下来的一行为一个正整数m,表示小Hi询问的次数,其后m行,每一行一个字符串,该字符串由不超过10个的小写英文字母组成,表示小Hi的一个询问。
在20%的数据中n, m<=10,词典的字母表大小<=2.
在60%的数据中n, m<=1000,词典的字母表大小<=5.
在100%的数据中n, m<=100000,词典的字母表大小<=26.
本题按通过的数据量排名哦~
输出
对于小Hi的每一个询问,输出一个整数Ans,表示词典中以小Hi给出的字符串为前缀的单词的个数。
样例输入
5babaabbabbbaaaaabbaaaaaabaababaababb5babbbaabaaababbbbbabbaab
样例输出
1
0
3
0
0
代码如下:(包括Trie树的建立,插入、查找)
#include <stdio.h>#include <iostream>#include <stdlib.h>using namespace std;class node{ public: node(){ for(int i=0;i<26;i++) next[i]=NULL; count=1; } node* next[26]; int count;};class tree{ public: tree(){ root=new node(); } void insert(string &word); int search(string &word); private: node *root;};void tree::insert(string &word){ int len=word.length(); node *proot=root; for(int i=0;i<len;i++){ if(proot->next[word[i]-'a']==NULL) proot->next[word[i]-'a']=new node(); else proot->next[word[i]-'a']->count++; proot=proot->next[word[i]-'a']; }}int tree::search(string &word){ int len=word.length(); node *proot=root; for(int i=0;i<len;i++){ if(proot->next[word[i]-'a']==NULL) return 0; proot=proot->next[word[i]-'a']; } return proot->count;}int main(){ int n; string word; tree T; cin>>n; while(n--){ cin>>word; T.insert(word); } cin>>n; while(n--){ cin>>word; cout<<T.search(word)<<endl; } return 0;}
0 0
- hihocoder 1014 Trie树 trie
- [hihoCoder 1014][Trie 树]Trie
- Trie树 hihoCoder
- hihoCoder- Trie树
- hihocoder 1014 trie树
- hihocoder Trie树
- hihocoder#1014 : Trie树
- hihoCoder #1014 : Trie树
- [HiHoCoder]#1014 : Trie树
- hihoCoder#1014Trie树
- hihoCoder #1014 Trie树
- hihocoder-#1014 : Trie树
- hihoCoder 1014 : Trie树
- hihoCoder #1014 : Trie树
- hihocoder-#1014 : Trie树
- hihocoder #1014 trie 树
- hihoCoder - 1014 - Trie树
- 【hihocoder】1014. Trie树
- UITabBarController自定义转场动画
- cuda二维数组内存分配和数据拷贝
- Displaying Graphics with OpenGL ES (六)——Responding to Touch Events
- unity 获取当前键盘按键
- Mybatis运用mapper代码实现和Spring的整合(一)
- Trie树 [Hihocoder]
- iOS开发中字符串的截取
- CI框架学习笔记(一)
- Oracle11gR2连接ArcGIS10.2.2配置问题
- js的移位运算以及二进制补码反码的计算方法
- 如何实现View的移动
- 关于 Android 进程保活,你所需要知道的一切
- Django笔记 富文本编辑器 课题
- USB驱动开发(一)