Links: http://blog.csdn.net/acvay/article/details/47089657
Test instructions gives you a set of phone numbers to determine if one of the phones is a prefix for another phone
The basic application of the dictionary tree can first put all the phones into the trie to mark the end character of each phone and then query each number to see if there is an end tag, which means that the number is the prefix of this number
Actually the insertion is done to know if a number is prefixed with another number, assuming a is a prefix of B.
If a is inserted before B, then insert B will encounter the end tag of a.
Weak A is inserted after B and then a node after the insert is complete and a non-empty child node.
This will only be used at each insert to determine the line can be omitted to eliminate the query this step
Pointers and dynamic memory allocation implementation dictionaries are easier, but not releasing memory when there are multiple sets of samples can cause the MLE to free up memory and spend more unnecessarily time on the tle, so it's better to use arrays.
#include <cstdio>#include<cstring>using namespacestd;Const intN =10005;Chartel[n][ A];intTrie[n *Ten][Ten], L;BOOLEnd[n *Ten], flag;voidInittrie () {L=1; memset (Trie,0,sizeof(trie)); memset (End,0,sizeof(end));}void_insert (Char*s) {intR =0, i =0, J; while(S[i]) {if(End[r]) flag =false; J= s[i++]-'0'; if(!Trie[r][j]) trie[r][j]= l++; R=Trie[r][j]; } for(inti =0; I <Ten; i++) { if(Trie[r][i]) flag =0; END[R]=1; }}intMain () {intT, N; scanf ("%d", &u); while(t--) {Inittrie (); scanf ("%d", &N); Flag=true; for(inti =0; I < n; ++i) {scanf ("%s", Tel[i]); _insert (Tel[i]); } puts (flag?"YES":"NO"); } return 0;}
View Code
HDU 1671 Phone List (trie array Implementation)