Title Link: http://hihocoder.com/problemset/problem/1014
1#include <iostream>2#include <cstdio>3#include <cstdlib>4#include <cstring>5 using namespacestd;6 7 Const intMAXN = 1e6 +5;//1e5 * Ten (1e5 words * word length (<=))8 Const intN = -;9 Ten intle; One CharStr[n]; A - structTrie - { the intCNT; - intNext[n]; - voidinit () { -CNT =0; +Memset (Next,-1,sizeof(next)); - } + }T[MAXN]; A at voidBuild (Char*s) - { - inti =0, p =0; - while(S[i]) { - intx = S[i]-'a'; - if(T[p].next[x] = =-1) { in t[le].init (); -T[P].NEXT[X] = le++; to //printf ("t[%d].next[%d] =%d,", p, X, T[p].next[x]); + } -p =T[p].next[x]; thet[p].cnt++; * //printf ("t[%d].cnt =%d\n", p, t[p].cnt); $i++;Panax Notoginseng } - } the + voidQuery (Char*s) A { the inti =0, p =0; + while(S[i]) { - intx = S[i]-'a'; $ if(T[p].next[x] = =-1) { $Puts"0"); - return ; - } thep =T[p].next[x]; -i++;Wuyi } theprintf"%d\n", t[p].cnt); - } Wu - intMain () About { $ intN, M; - #ifndef Online_judge -Freopen ("In.txt","R", stdin); - #endif //Online_judge A + while(SCANF ("%d", &n)! =EOF) { theLe =1; -t[0].init ();//! Root $ while(n--) { thescanf"%s", str); the Build (str); the } thescanf"%d", &m); - while(m--) { inscanf"%s", str); the Query (str); the } About } the return 0; the}
Hiho, Trie tree for the second week.