(Hdu step 5.2.2) statistical difficulties (calculate the number of words prefixed with a certain word in a bunch of words), hdu5.2.2

Source: Internet
Author: User

(Hdu step 5.2.2) statistical difficulties (calculate the number of words prefixed with a certain word in a bunch of words), hdu5.2.2

Question:

Statistical difficulties
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)
Total Submission (s): 349 Accepted Submission (s): 209
 
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
For each question, the number of words prefixed with this string is given.
Sample Input
bananabandbeeabsoluteacmbabbandabc
 
Sample Output
2310
 
AuthorIgnatius. L
 
RecommendIgnatius. L


Question Analysis:

The basic topic of Trie. Use C ++ to submit this question, and use G ++ to submit MLE. And hangdian is sometimes funny.

Soon, I typed it out. After submitting it, I kept fighting. Later I handed it back at noon, and it was normal, around Ms. If it's TLE, just pay it a few more times ..



The Code is as follows:

/** B. cpp ** Created on: March 8, 2015 * Author: Administrator */# include <iostream> # include <cstdio> # include <cstring> using namespace std; const int MAX = 26; typedef struct TrieNode {// Trie node int prefix; // The number of prefixes to the current node struct TrieNode * next [MAX]; // the current node's child node} Trie; /*** Trie tree insert operation: * insert string s to the dictionary tree with root **/void insert (Trie * root, char * s) {// if the root of the current dictionary tree is blank | if (root = NULL | * s = '\ 0') {return; // return directly Back} Trie * p = root; while (* s! = '\ 0') {// continuously traverse every character of the string to be inserted if (p-> next [* s-'a'] = NULL) {// if no node exists in the dictionary tree, Trie * temp = (Trie *) malloc (sizeof (Trie) will be created for the node )); int I; for (I = 0; I <MAX; ++ I) {temp-> next [I] = NULL;} temp-> prefix = 1; // The default number of prefixes after the node is created is 1 p-> next [* s-'a'] = temp; // use next to point to tempp = p-> next [* s-'a']; // traverse the next node} else {// If the node already exists in the dictionary tree p = p-> next [* s-'a']; // traverse the next node p-> prefix ++; // The number of prefixes to the node + 1} s ++; // traverse the next character of the string}/*** in the root dictionary tree Find the number of words prefixed with pre */int count (Trie * root, char * pre) {Trie * p = root; while (* pre! = '\ 0') {// continuously traverse the prefix string if (p-> next [* pre-'a'] = NULL) {// if the child node of a node does not contain the return 0 character currently traversed by pre; // It indicates that the word is not prefixed with pre, directly return 0} else {// if the child node of the current node contains the characters that are currently traversed by pre, then continue to traverse p = p-> next [* pre-'a']; pre ++ ;}} return p-> prefix; // returns the number of words starting with pre}/*** dictionary tree release operation */void del (Trie * root) {int I; for (I = 0; I <MAX; ++ I) {if (root-> next [I]! = NULL) {del (root-> next [I]) ;}} free (root) ;}int main () {char str [11]; // Trie * root = (Trie *) malloc (sizeof (Trie); int I; for (I = 0; I <MAX; ++ I) {root-> next [I] = NULL;} root-> prefix = 0; while (gets (str) {// read the string if (strcmp (str, "") = 0) {// If the input is an empty string break; // The system jumps out of the loop} insert (root, str ); // Insert the currently read string into the dictionary tree} while (gets (str) {printf ("% d \ n", count (root, str ));} del (root); // if this operation does not take about ms. If this operation is added, it takes about Ms. 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.