HDU 1671 Phone List (trie array Implementation)

Source: Internet
Author: User

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)

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.