hdu 1251 statistical problems (dictionary tree <PS: C++ submission must not burst memory>)

统计难题
Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)
Total Submission(s): 50524    Accepted Submission(s): 17827


Problem Description
Ignatius recently encountered a problem, the teacher gave him a lot of words (only lowercase letters, no repeated words), and now the teacher asked him to count the number of words prefixed with a certain string (the word itself is also own prefix).
 

Input
The first part of the input data is a word list, one word per line, the length of the words does not exceed 10, they represent the words that the teacher handed to Ignatius for statistics, and an empty line represents the end of the word list. The second part is a series of The questions, one question per line, each question is a string.

Note: There is only one set of test data for this question, and it will be processed until the end of the file.
 

Output
For each question, gives the number of words prefixed by this string.
 

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:  1075 1247 1671 1298 1800

1  /* *
 2      Algorithm: Dictionary tree
 3      
4      Description:
 5          On the basis of the dictionary tree template, every step of insertion should be performed p->cnt++
 6          Others are consistent with the original template of the dictionary tree
 7  * */

Core code:

 1 struct node
 2 {
 3     node *next[26];
 4     int cnt;
 5     node()
 6     {
 7         cnt = 0;
 8         memset(next, 0, sizeof(next));
 9     }
10 };
11 
12 node *root = new node();
13 
14 int my_find(char *s)
15 {
16     node *p = root;
17     int i, k, len = strlen(s);
18     for (i = 0; i < len; ++ i)
19     {
20         k = s[i] - 'a';
21         if (p->next[k] == NULL) return 0;
22         p = p->next[k];
23     }
24     return p->cnt;
25 }
26 
27 void my_insert(char *s)
28 {
29     node *p = root;
30     int i, k, len = strlen(s);
31     for (i = 0; i < len; ++ i)
32     {
33         k = s[i] - 'a';
34         if (p->next[k] == NULL)
35             p->next[k] = new node();
36         p = p->next[k];
37         p->cnt ++;
38     }
39     return ;
40 }

 C/C++ code implementation (AC):

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>

using namespace std;

int flag = 0, len;

char temp[15];

struct node
{
    node *next[26];
    int cnt;
    node()
    {
        cnt = 0;
        memset(next, 0, sizeof(next));
    }
};

node *root = new node();

int my_find(char *s)
{
    node *p = root;
    int i, k, len = strlen(s);
    for (i = 0; i < len; ++ i)
    {
        k = s[i] - 'a';
        if (p->next[k] == NULL) return 0;
        p = p->next[k];
    }
    return p->cnt;
}

void my_insert(char *s)
{
    node *p = root;
    int i, k, len = strlen(s);
    for (i = 0; i < len; ++ i)
    {
        k = s[i] - 'a';
        if (p->next[k] == NULL)
            p->next[k] = new node();
        p = p->next[k];
        p->cnt ++;
    }
    return ;
}

intmain ()
{
    while (gets(temp))
    {
        if (temp[0] == NULL)
        {
            flag = 1;
            continue;
        }

        if (!flag)
            my_insert(temp);
        else
            printf("%d\n", my_find(temp));
    }
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325234561&siteId=291194637