[ACM] hdu 1251 statistical difficulties (Dictionary tree)

Source: Internet
Author: User

Statistical difficulties

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 Input
bananabandbeeabsoluteacmbabbandabc

Sample Output
2310

AuthorIgnatius. L


Solution:

This is the first time that we have made a dictionary tree. Dictionary tree Baidu Encyclopedia: Also known as the word search tree and Trie tree, is a tree structure and a variant of the hash tree. A typical application is used for statistics, sorting, and saving a large number of strings (but not limited to strings). Therefore, it is often used by the search engine system for text word frequency statistics. The advantage is that the common prefix of a string is used to reduce the query time and minimize unnecessary string comparisons. The query efficiency is higher than that of a hash table.

After learning the dictionary tree, I think it is obvious that we use space for time, which is very complex. For example, the number of dictionaries is only 26 lower-case letters, the child node of each node has 26 child nodes, and each layer of the dictionary tree retains the same letters of different words.

For better illustration, assuming that all words only contain four letters a, B, c, and d, the tree is created in this way.


The topic is to count the number of words prefixed with a character string. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + tPrC66O6PC9wPgo8cD48L3A + CjxwcmUgY2xhc3M9 "brush: java;"> # include # Include # Include # Include Using namespace std; char str [12]; const int maxn = 26; // The maximum number of child nodes. If there are only 26 letters, use 26. Struct Trie // tree struct {int cnt; // stores the number of occurrences of a letter Trie * next [maxn]; // The number of children corresponding to each node, if there are only 26 letters, you can use 26.}; Trie root;/* void init (Trie t) {for (int I = 0; I <26; I ++) t. next [I] = NULL;} * // you do not need to initialize void CreateTrie (char * str) {int len = strlen (str) on the Root Node separately ); trie * p = & root, * q; for (int I = 0; I Next [id] = NULL) // The first encounter {q = (Trie *) malloc (sizeof (Trie); q-> cnt = 1; // At the beginning, the error is written as q-> cnt ++; for (int I = 0; I Next [I] = NULL; // initialize the child node of a non-empty node p-> next [id] = q; // enter p = p-> next [id] in the tree. // at this time, P is not an empty node} else {p-> next [id]-> cnt ++; // not the first time, number + + p = p-> next [id] ;}} int find (char * str) {int len = strlen (str); Trie * p = & root; for (int I = 0; I Next [id]; // keep going down. If (p = NULL) // The word cannot be found. At the beginning, an error was written in p-> next [id] = NULL return 0 ;} return p-> cnt;} int main () {while (gets (str) & str [0]! = '\ 0') {CreateTrie (str);} while (scanf ("% s", str )! = EOF) {printf ("% d \ n", find (str) ;}return 0 ;}


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.