(Hdu step 5.2.3) Phone List (Trie implementation: In a bunch of numbers, determine if there is a number is the prefix of other numbers)

Source: Internet
Author: User


Topic:

Phone List
Time limit:3000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 239 Accepted Submission (s): 96
Problem Descriptiongiven A list of phone numbers, determine if it is consistent in the sense this no number is the prefix of another. Let ' s say the phone catalogue listed these numbers:
1. Emergency 911
2. Alice 97 625 999
3. Bob 91 12 54 26
In this case, it's not possible-to-call Bob, because-the central would direct your-to-the-emergency line as soon as Y OU had dialled the first three digits of Bob ' s phone number. So the list would not being consistent.
Inputthe first line of input gives a single integer, 1 <= t <=, the number of test cases. Each test case is starts with N, the number of the phone numbers, on a separate line, 1 <= n <= 10000. Then follows n lines with a unique phone number on each line. A phone number is a sequence of at the most ten digits.
Outputfor each test case with output "YES" If the list is consistent, or "NO" otherwise.
Sample Input
2391197625999911254265113123401234401234598346
Sample Output
NOYES
Source2008 "Insigma International Cup" Zhejiang Collegiate Programming Contest-warm up (3)
Recommendlcy

Topic Analysis:

The previous blog introduces the non-trie approach to the problem. So this article introduces the design of the problem: the basic use of trie-the application of prefix matching.

The following is a brief introduction to Trie's basic knowledge (personal understanding, please accept critically).


1. Nature

1) root node does not contain any characters

2) All child nodes of a node contain different characters.

3) The path that passes from the root node to a node is the word that can be formed so far.


2. Basic operation

1) Add:

Assume that the root node of the string Str,trie tree is root. I=0,p=root.

1) Take str[i], determine whether p->next[str[i]-97] is empty, if empty, establish node temp, and p->next[str[i]-97] point to Temp, and then P point to temp; Then p=p->next[str[i]-97];

2) i++, continue to take Str[i], loop 1), until the Terminator ' \ ' is encountered, at which point Isstr in the current node p is set to true.

void Insert (trie* root,char* s) {if (root = = NULL | | s = = ' + ') {return;}  trie* p = root;while (*s! = ') {if (p->next[*s-' a '] = = NULL) {trie* temp = malloc (sizeof (Trie)); int i;for (i = 0; i < MAX; ++i) {Temp->next[i] = NULL;} Temp->isstr = false;p->next[*s-' a '] = Temp;p = p->next[*s-' a '];} Else{p = p->next[*s-' a '];} s++;} P->isstr = true;}



2) Enquiry:

Assume that the string you are looking for is the root node of the Str,trie tree Root,i=0,p=root

1) Take str[i], Judge p->next[str[i]-97] is empty, if empty, return false, if not empty, then p=p->next[str[i]-97], continue to take the character.

2) repeat 1) until the Terminator ' Isstr ' is encountered and returns true if the current node p is not null and true, otherwise false.

BOOL Search (trie* root,char* s) {trie* p = root;while (P! = NULL && *s! = ' + ') {p = p->next[*s-' a '];s++;} Return (P! = NULL && p->isstr);}



3) Delete:

The deletion is done in a recursive manner.

void del (trie* root) {int i;for (i = 0; i < MAX; ++i) {if (Root->next[i]! = NULL) {del (root->next[i]);}} Free (root);}



3, the realization of the basic ideas

1) Start a search from the root node

2) Remove the first character of the keyword and select the appropriate subtree according to the character, then go to the appropriate subtree to continue searching

3) Repeat 2) Operation Guide to complete the search

4) at a node, if all the characters of the keyword have been removed. The additional information for that node is read out.



The code is as follows:

/* * c2.cpp * * Created on:2015 March 7 * author:administrator * * #include <iostream> #include <cstdio>usin G namespace Std;const int MAX = 10;//number of child nodes per node bool flag;//is used to mark the presence of a number that is the prefix of another number typedef struct TRIENODE{// The Dictionary tree Trie node bool isstr;//is used to mark the insertion of a word struct trienode* next[max];//the child node of each node}trie;/** * Dictionary tree (prefix tree) Trie (insert operation *) Insert the string s into the root-rooted dictionary tree */void Insert (trie* root,char* s) {if (root = NULL | | s = = ')} {//If this dictionary tree is empty | | The string to be inserted is null return;//return directly}trie* p = root;while (*s! = ') {//sequentially traverse the string to be inserted if (p->next[*s-' 0 '] = = NULL) {// If the current child node does not exist//then new node trie* temp = (trie*) malloc (sizeof (Trie)); int i;for (i = 0; i < MAX; ++i) {Temp->next[i] = NULL ;} Temp->isstr = false;p->next[*s-' 0 '] = temp;//points the child node in p to the newly created temp junction p = p->next[*s-' 0 '];//update the node currently found in the Dictionary tree} else{p = p->next[*s-' 0 '];//update the node currently found in the Dictionary tree}if (P->isstr = True) {//If the currently found node is the end node of a previous word, Indicates that there is a prefix for a number that is another number. Flag = true;//Flags the flag as true. Indicates that there is a prefix for a number that is another number return; return}//If you can continue to execute the following code, Indicates that no number exists until the current node is the other one.The prefix of the number. S++;//continues to traverse the next node}p->isstr = true;//string has been inserted. Mark the isstr of the node as true, indicating that a number int i;for can be formed here (i = 0; i < MAX; ++i {//Traverse all child nodes of the node if (p->next[i]! = NULL) {//If the child node exists. Flag = true;//Indicates that the number is a prefix for a different number. Mark flag as truebreak;//bounce Loop}}}/** * Trie of the Dictionary tree (prefix tree) * If the s string exists in the root-rooted dictionary tree. Returns True, * otherwise returns false */bool search (trie* root,char* s) {trie* p = root;while (P! = NULL && *s! = ' + ') {//If the currently traversed node is not null&& string s has not been traversed//then continues to traverse p = p->next[*s-' 0 '];s++;} Return (P! = NULL && p->isstr);//Determine if the end node exists && whether to form a number in the node}/** * Delete operation of the dictionary tree (prefix tree) Trie * */void del (trie* root) {int i;for (i = 0; i < MAX; ++i) {//Traverse all child nodes of the node if (root->next[i]! = NULL) {//If the child node is not Nulldel (Root->next[i]) ;//recursively delete the child node}}free (root);//Free Space}int main () {int t;scanf ("%d", &t); char s[11];while (t--) {flag = false;// Initialization operation trie* root = (trie*) malloc (sizeof (Trie)), root->isstr = False;int i;for (i = 0; i < MAX; ++i) {Root->next[i] = NULL;} int n;scanf ("%d", &n); for (i = 0; i < n; ++i) {scanf ("%s", s), if (flag = = False) {//If a number is not currently present in the case of a prefix of another number insert (Root,s);}} if (flag = = True) {printf ("no\n");} else{printf ("yes\n");} del (root);//Free up space. If you do not free space here, it will cause memory explosion and last Mle}return 0;}














(Hdu step 5.2.3) Phone List (Trie implementation: In a bunch of numbers, determine if there is a number is the prefix of other numbers)

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.