HDU 1251 statistical difficulties (basic dictionary tree template with detailed comments)

Source: Internet
Author: User

Link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1251

 

The meaning of the question has been quite clear: count the number of words prefixed with a certain string (the word itself is also its own prefix ).

Now, I have directly provided some comments to the basic template of the dictionary tree. I hope you can understand it.

 

# Include <iostream> # include <cstring> using namespace STD; typedef struct tree // create a node {int num; struct tree * BR [26]; // each layer has 26 letters} node; node * head; void tree_insert (char STR []) // build {node * t, * s = head; int I, j; int Len = strlen (STR); for (I = 0; I <Len; I ++) {int id = STR [I]-'A '; if (S-> Br [ID] = NULL) {T = new node; // create a new node under this letter for (j = 0; j <= 25; j ++) // initialize the new node {T-> Br [J] = NULL;} t-> num = 0; s-> Br [ID] = T; // sub-tree pointer marking the node link} s = s-> Br [ID]; // s points to the next node S-> num ++; // number of times this vertex has passed} int tree_find (char STR []) // the search process, search {node * s = head; int count; int Len = strlen (STR); For (INT I = 0; I <Len; I ++) from each letter) {int id = STR [I]-'A'; If (S-> Br [ID] = NULL) {COUNT = 0; return count ;} else {S = s-> Br [ID]; Count = s-> num;} return count;} int main () {int I; head = new node; // header node for (I = 0; I <= 25; I ++) // 26 nodes, initialize {head-> Br [I] = NULL; head-> num = 0;} Char TEM [15]; while (gets (TEM), strcmp (TEM, "") {tree_insert (TEM ); //} while (gets (TEM) {cout <tree_find (TEM) <Endl;} 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.