【hihocoder】#1014 : Trie树
2018-03-06 21:59
375 查看
题目链接
https://hihocoder.com/problemset/problem/1014题目思路
该题就是Trie树的应用,只要掌握Trie树的维护和查询就可以了.代码如下
#include <iostream> using namespace std; const int maxn = 10+5; struct TrieNode { int num; TrieNode* next[26]; TrieNode(){ num=0; for(int i=0;i<26;i++) { next[i]=NULL; } } }; struct TrieNode root; void Insert(char ch[]) { struct TrieNode *p = &root; for(int i=0;ch[i];i++) { int idx = ch[i]-'a'; if(p->next[idx]==NULL) p->next[idx]=new struct TrieNode; p=p->next[idx]; p->num++; } } int Select(char ch[]) { struct TrieNode *p = &root; for(int i=0;ch[i];i++) { int idx = ch[i]-'a'; if(p->next[idx]==NULL) return 0; p=p->next[idx]; } return p->num; } int main() { int n,m; cin>>n; char ch[maxn]; for(int i=0;i<n;i++) { cin>>ch; Insert(ch); } cin>>m; for(int i=0;i<m;i++) { cin>>ch; cout<<Select(ch)<<endl; } return 0; }
相关文章推荐
- hihocoder1014 Trie树
- hihoCoder 1014 Trie树(字典树入门)
- hihoCoder 1014 Trie树 (Trie)
- hihoCoder 1014 Trie树
- hihoCoder 1014 : Trie树
- hihocoder1014 Trie树
- hihoCoder 1014 Trie树 题解
- hihoCoder 1014 Trie树
- hihoCoder hiho一下 第二周 #1014 : Trie树(Trie树基本应用)
- hihoCoder 1014 Trie树 (Trie)
- hihocoder1014 : Trie树
- hihoCoder 1014 Trie树(字典树)
- hihoCoder 1014 Trie树
- hihoCoder 1014:Trie树
- hihoCoder 1014 Trie树
- hihocoder 1014 Trie树 字典树
- hihocoder 1014 trie树
- hihoCoder 1014 Trie树
- 1014 : Trie树 hihocoder
- 【hihocoder】1014 : Trie树