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)