• HDU 1251 统计难题(字典树)


    统计难题

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)
    Total Submission(s): 48444    Accepted Submission(s): 17131


    Problem Description
    Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).
     
    Input
    输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串.

    注意:本题只有一组测试数据,处理到文件结束.
     
    Output
    对于每个提问,给出以该字符串为前缀的单词的数量.
     
    Sample Input
    banana band bee absolute acm ba b band abc
     
    Sample Output
    2 3 1 0
     
    Author
    Ignatius.L
     
    Recommend
    Ignatius.L   |   We have carefully selected several similar problems for you:  2846 1166 2222 1020 1305 
     
     1 #include <iostream>
     2 #include <cstring>
     3 #include <string>
     4 #include <algorithm>
     5 #include <cstdio>
     6 using namespace std;
     7 typedef struct nn
     8 {
     9     int s;
    10     nn *nxt[26];
    11 }node;
    12 node*builde()
    13 {
    14     node*p = (node*)malloc(sizeof(node));
    15     p->s = 0;
    16     int i;
    17     for (i = 0; i<26; i++)
    18     {
    19         p->nxt[i] = NULL;
    20     }
    21     return p;
    22 }
    23 node *root = builde();
    24 int l;
    25 
    26 void insert(char *c)//不能写成node*insert....
    27 {
    28     int i;
    29     node *p = root;
    30     for (i = 0; i<l; i++)
    31     {
    32         if (p->nxt[c[i] - 'a'] == NULL)
    33         {
    34             p->nxt[c[i] - 'a'] = builde();
    35         }
    36         p = p->nxt[c[i] - 'a'];
    37         p->s++;
    38     }
    39 }
    40 
    41 int find(char *c)
    42 {
    43     int i;
    44     node *p = root;
    45     for (i = 0; i<l; i++)
    46     {
    47         if (p->nxt[c[i] - 'a'] == NULL)
    48             return 0;
    49         p = p->nxt[c[i] - 'a'];
    50     }
    51     return p->s;
    52 }
    53 
    54 int main()
    55 {
    56     char a[15];
    57     while (gets(a))
    58     {
    59         l = strlen(a);
    60         if (l == 0) break;//gets的使用
    61         insert(a);
    62     }
    63     while (gets(a))
    64     {
    65         l = strlen(a);
    66         int ss = find(a);
    67         cout << ss << endl;
    68     }
    69     return 0;
    70 }
  • 相关阅读:
    DTS和AC3的区别
    bind出现 file does not end with newline错误
    删除桌面菜单多余项
    you have requested a nonexistent service "grid"
    php 常用函数
    Jquery
    Twig 的Filters学习
    Twig 的 tags学习(中文) 之三 完结
    PHP 正则表达式
    SQL处理字符串
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/8452348.html
Copyright © 2020-2023  润新知