[HDOJ1251] statistical difficulties and hdoj1251 statistical difficulties

Source: Internet
Author: User

[HDOJ1251] statistical difficulties and hdoj1251 statistical difficulties

Question link: http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 1251

 

 

Statistical difficulties

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


Problem DescriptionIgnatius recently encountered a Problem. The teacher gave him many words (only lowercase letters are used and no duplicate words will appear ), the teacher asks him to calculate the number of words prefixed with a certain string (the word itself is also its own prefix ).

 

The first part of Input data is a word table. Each line has one word. The length of a word cannot exceed 10. These words represent words that the teacher gave to Ignatius for statistics. A blank line indicates the end of the word table. the second part is a series of questions. Each question in each row is a string.

Note: This question only contains a set of test data, which is processed until the end of the file.

 

Output provides the number of words prefixed with this string for each question.

 

Sample Inputbananabandbeeabsoluteacmbabbandabc

 

Sample Output2310 dictionary tree, which is not well written, because it consumes a lot of memory when it is new. When compiled with G ++, MLE is also cracked. C ++. The specific method is to add 1 to the node at the corresponding position when inserting characters into the dictionary tree, indicating that the current node has been crossed once again. Output the cnt value corresponding to the last character of the string when searching. Note that this word does not exist.
 1 #include <cstdio> 2 #include <cstdlib> 3 #include <cstring> 4 #include <algorithm> 5 #include <iostream> 6 #include <cmath> 7 #include <queue> 8 #include <map> 9 #include <stack>10 #include <list>11 #include <vector>12 13 using namespace std;14 15 char str[11];16 typedef struct Node {17     Node *next[26];18     int cnt;19     Node() {20         cnt = 0;21         for(int i = 0; i < 26; i++) {22             next[i] = NULL;23         }24     }25 }Node;26 27 void insert(Node *p, char *str) {28     for(int i = 0; str[i]; i++) {29         int t = str[i] - 'a';30         if(p->next[t] == NULL) {31             p->next[t] = new Node;32         }33         p = p->next[t];34         p->cnt++;35     }36 }37 38 int find(Node *p, char *str) {39     for(int i = 0; str[i]; i++) {40         int t = str[i] - 'a';41         p = p->next[t];42         if(!p) {43             return 0;44         }45     }46     return p->cnt;47 }48 49 int main() {50     Node *root = new Node();51     while(gets(str) && strlen(str)) {52         insert(root, str);53     }54     while(gets(str)) {55         int ans = find(root, str);56         printf("%d\n", ans);57     }58     return 0;59 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.