hiho一下第二周——字典树

来源:互联网 发布:python asyncio.wait 编辑:程序博客网 时间:2024/06/05 07:53

hiho一下第二周——字典树

#include <iostream>  #include <stack>#include <vector>#include <algorithm>#include <queue>#include <map>#include <cstdio>    #include <cstring>using namespace std; typedef struct Trie_node  {      int count;                    // 统计单词前缀出现的次数      struct Trie_node* next[26];   // 指向各个子树的指针      bool exist;                   // 标记该结点处是否构成单词    }TrieNode , *Trie;TrieNode* createTrieNode()  {      TrieNode* node = (TrieNode *)malloc(sizeof(TrieNode));      node->count = 0;      node->exist = false;      memset(node->next , 0 , sizeof(node->next));    // 初始化为空指针      return node;  }void Trie_insert(Trie root, char* word)  {      Trie node = root;      char *p = word;      int id;      while( *p )      {          id = *p - 'a';          if(node->next[id] == NULL)          {              node->next[id] = createTrieNode();          }          node = node->next[id];  // 每插入一步,相当于有一个新串经过,指针向下移动          ++p;          node->count += 1;      // 这行代码用于统计每个单词前缀出现的次数(也包括统计每个单词出现的次数)      }      node->exist = true;        // 单词结束的地方标记此处可以构成一个单词  } int Trie_search(Trie root, char* word)  {      Trie node = root;      char *p = word;      int id;      while( *p )      {          id = *p - 'a';          node = node->next[id];          ++p;          if(node == NULL)              return 0;      }      return node->count;  }  int main()  {  int argc1,argc2,ans;    int i = 0;    char *argv1,*argv2;    //freopen("data.txt","r",stdin);    Trie root = createTrieNode();     // 初始化字典树的根节点      char str[12] ;      bool flag = false;     cin >>  argc1;    while(i < argc1 && cin >>str)      {          Trie_insert(root , str);         //cout << str << endl;        i++;     }     i = 0;    cin >>  argc2;    while(i <= argc2 && cin >> str)      {           printf("%d\n",Trie_search(root , str));        i++;    }     //system("pause");      return 0;  }  

0 0
原创粉丝点击