1#include <iostream>2#include <cstring>3#include <cstdio>4#include <cstdlib>5#include <algorithm>6 using namespacestd;7 structTree8 {9Tree *next[94];Ten BOOLIsvirus; One intnum; A }; -Tree *Root; - intAll ; the Chartemp[10001]; - inttemps[10001]; - intn,m; - voidInsertCharTemp[],intNum) + { - intlen=strlen (temp); +Tree *the=Root; A for(intI=0; i<len; i++) at { - inttemps=temp[i]-' '; - if(the->next[temps]==NULL) - { -tree* ttemp= (Tree *)malloc(sizeof(Tree)); - for(intj=0; j<94; J + +) inttemp->next[j]=NULL; -ttemp->isvirus=false; to if(i==len-1) + { -ttemp->isvirus=true; thettemp->num=Num; * } $the->next[temps]=ttemp;Panax Notoginseng } - Else if(i==len-1) the { +the->next[temps]->isvirus=true; Athe->next[temps]->num=Num; the } +The=the->Next[temps]; - } $ } $ voidSearchintnum) - { - intvirusnum=0; the intvir[3]; - intlen=strlen (temp);Wuyi for(intI=0; i<len; i++) the { -tree* the=Root; Wu for(intj=0; i+j<len; J + +) - { About intTtemp= (int) (temp[i+j]-' '); $ if(The->next[ttemp]==null) Break; - Else if(the->next[ttemp]->Isvirus) - { - BOOLok=false; A for(ints=0; s<virusnum; s++) + { the if(vir[s]==the->next[ttemp]->num) -ok=true; $ } the if(!OK) the { theVir[virusnum++]=the->next[ttemp]->num; thei=i+J; - Break; in } the Else theThe=the->Next[ttemp]; About } the Else theThe=the->Next[ttemp]; the } + if(virusnum==3) Break; - } the if(Virusnum)Bayi { theall++; theprintf"Web%d:", num); -Sort (vir,vir+virusnum); - for(intI=0; i<virusnum; i++) theprintf"%d", Vir[i]); theprintf"\ n"); the } the } - intMain () the { the intVirusnum; the intvir[3];94root= (Tree *)malloc(sizeof(Tree)); the for(intI=0; i<94; i++) theroot->next[i]=NULL; theroot->isvirus=false;98Cin>>N; About for(intI=1; i<=n; i++) - {101scanf"%s", temp);102 Insert (temp,i);103 }104Cin>>m; the for(intI=1; i<=m; i++)106 {107scanf"%s", temp);108 search (i);109 } theprintf"Total:%d\n", all);111 return 0; the}
View Code
The subject is very water, the direct dictionary tree can be a drop, but heard is the AC automaton template problem, have learned the idea of AC automata, the back may be affixed to the code of AC automata.
HDU 2896 Dictionary Tree Solution