zoukankan      html  css  js  c++  java
  • HDU 1251 统计难题(字典树)

                                         统计难题

                                                      Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)

                                                                           Total Submission(s): 12855    Accepted Submission(s): 5433

    Problem Description
    Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).
     
    Input
    输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串.
    注意:本题只有一组测试数据,处理到文件结束.
     
    Output
    对于每个提问,给出以该字符串为前缀的单词的数量.
     
    Sample Input
    banana
    band
    bee
    absolute
    acm
     
    ba
    b
    band
    abc
     
    Sample Output
    2
    3
    1
    0
     
     
     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <string.h>
     4 
     5 typedef struct _TrieTree
     6 {
     7     int  count;
     8     bool flag;
     9     struct _TrieTree *next[26];
    10 
    11 }TrieNode;
    12 
    13 TrieNode *CreateTrieTree()   //创建字典树
    14 {
    15     TrieNode *node = (TrieNode *)malloc(sizeof(TrieNode));
    16     node->count = 0;
    17     node->flag = false;
    18     for(int i = 0; i < 26; i++)
    19         node->next[i] = NULL;
    20 
    21     return node;
    22 }
    23 
    24 void InsertTrieTree(TrieNode *root, char word[]) //插入字符串
    25 {
    26     TrieNode *p = root;
    27     int length = strlen(word);
    28     for(int i = 0; i < length; i++)
    29     {
    30         int index = word[i] - 97;
    31         if(p->next[index] == NULL)
    32         {
    33             p->next[index] = CreateTrieTree();
    34         }
    35         p = p->next[index];
    36         p->count++;
    37     }
    38     p->flag = true;
    39 }
    40 
    41 int QueryTrieTree(TrieNode *root, char word[])   //查询字符串
    42 {
    43     TrieNode *p = root;
    44     int length = strlen(word);
    45     for(int i = 0; i < length; i++)
    46     {
    47         int index = word[i] - 97;
    48         if(p->next[index] == NULL)
    49             return 0;
    50 
    51         p = p->next[index];
    52     }
    53 
    54     return p->count;
    55 }
    56 
    57 int main(int argc, char* argv[])
    58 {
    59     const int WORD_LEN = 15;
    60     char word[WORD_LEN];
    61 
    62     TrieNode *root = CreateTrieTree();
    63 
    64     while(gets(word))
    65     {
    66         if(strlen(word) == 0)
    67             break;
    68         
    69         InsertTrieTree(root, word);
    70     }
    71 
    72     while(scanf("%s", word) != EOF)
    73     {
    74         int cnt = QueryTrieTree(root, word);
    75 
    76         printf("%d\n", cnt);
    77     }
    78 
    79     return 0;
    80 }
  • 相关阅读:
    Linux基础之计算机硬件
    python中 __cmp__
    python中 __str__和__repr__
    python的构造函数传入任意数量的参数
    python中的偏函数
    javascript正则表达式
    js实现复选框的全选、全部选和反选
    js中的函数对象
    js中的作用域
    js中的arguments
  • 原文地址:https://www.cnblogs.com/Dreamcaihao/p/3095420.html
Copyright © 2011-2022 走看看