HDU 1251 (simple application of tire tree)

Source: Internet
Author: User

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1251

Test instructions: gives you multiple strings, the number of strings prefixed by a string.

Idea: Simple tire number application, in the tire data structure to add a storage to the current node string occurrences, in the process of the insertion to maintain.

Code

1#include <cstdio>2#include <cstring>3 Const intKIND = -;4 structTirenode5 {6     intNum//The number of occurrences of a string traversed to the node .7tirenode*Next[kind];8 Tirenode ()9     {Tennum =1; One          for(inti =0; i < KIND; ++i) Next[i] =NULL; A     } - }; -  thetirenode* root =NULL; -  - voidInsert (Char*x) - { +tirenode* temp =Root; -     if(Root = NULL)//Pre-award +     { Atemp =NewTirenode (); atRoot =temp; -     } -     intLen =strlen (x); -      for(inti =0; i < Len; ++i) -     { -         intCurr = X[i]-'a'; in         if(Temp->next[curr]! = NULL) + + (temp->next[curr]->num);//already exists -         ElseTemp->next[curr] =NewTirenode ();//does not exist totemp = temp->Next[curr]; +     } - } the  * intSearch (Char*x) $ {Panax Notoginseng     intRET =0; -     if(Root = NULL)returnRet//Pre-award thetirenode* temp =Root; +     intLen =strlen (x); A      for(inti =0; i < Len; ++i) the     { +         intCurr = X[i]-'a'; -         if(Temp->next[curr] = = NULL)return 0; $temp = temp->Next[curr]; $RET = temp->num; -     } -     returnret; the } - Wuyi voidRelease (tirenode*root) the { -     if(NULL = = root)return; Wu      for(inti =0; i < KIND; ++i) -     { About         if(Root->next[i]! =NULL) $Release (root->next[i]); -     } -     DeleteRoot; -Root =NULL; A } +  the intMain () - { $     Charstr[Ten]; the      while(gets (str)) the     { the         if(str[0] ==' /') the         { -              while(gets (str)) inprintf"%d\n", Search (str)); the         } the         ElseInsert (str); About     } the Release (root); the     return 0; the}

HDU 1251 (simple application of tire tree)

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.