设为首页 加入收藏

TOP

HihoCoder――Trie树
2015-07-20 18:07:14 来源: 作者: 【 】 浏览:4
Tags:HihoCoder Trie

?

题解:使用Trie树。。基础题目。一开始使用memset(child, 0, sizeof(child))总是MLE,应该是NULL定义的问题。指针直接为0是不合适的。

?

代码:

?

#include 
  
   
#include 
   
     #include 
    
      using namespace std; string str; struct TrieNode { TrieNode *child[26]; // int num; TrieNode() { num = 0; memset(child, NULL, sizeof(child)); } }; TrieNode *root; int temp; void Build(string s) { TrieNode *p = root; for(int i = 0; i < s.length(); i++) { temp = s[i]-'a'; // cout << temp << endl; if(p->child[temp] == NULL) { p->child[temp] = new TrieNode; } p = p->child[temp]; p->num ++; } } int check(string s) { TrieNode *p = root; for(int i = 0; i < s.length(); i++) { temp = s[i]-'a'; if(p->child[temp] == NULL) return 0; p = p->child[temp]; } return p->num; } int main() { int n, m; scanf(%d, &n); root = new TrieNode; while(n--) { cin >> str; Build(str); } scanf(%d, &m); while(m--) { cin >> str; cout << check(str) << endl; } return 0; } 
    
   
  


?

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇CodeForces 444C DZY Loves Colors 下一篇UVA 23 out of 5

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: