HDU Phone List dictionary tree prefix judgment

Source: Internet
Author: User
Tags character set strlen

Chain: http://acm.hdu.edu.cn/showproblem.php?pid=1671


Phone List Time limit:3000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 12763 Accepted Submission (s): 4341


Problem Description Given 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.

Input the 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.
Output for each test case, output "YES" If the list is consistent, or "NO" otherwise.
Sample Input

2 3 911 97625999 91125426 5 113 12340 123440 12345 98346
Sample Output
NO YES


Test instructions: Gives a string of n strings, asking if there will be a string that is prefixed with another string.

Practice: Constantly add to the trie, and then constantly judge whether the prefix of other strings, or the existing is not their own prefix.



#include <stdio.h> #include <string.h> #include <stdlib.h> #define MAX struct Trie {Trie *ne   
    Xt[max];   int V;   
 
Change as needed};

Trie Root;
    void Createtrie (char *str) {int len = strlen (str);
    Trie *p = &root, *q;
        for (int i=0; i<len; ++i) {int id = str[i]-' 0 ';
            if (p->next[id] = = NULL) {q = (Trie *) malloc (sizeof (Trie));    Q->v = 1;
            Initial v==1 for (int j=0; j<max; ++j) q->next[j] = NULL;
            P->next[id] = q;
        p = p->next[id];
            } else {p->next[id]->v++;
        p = p->next[id];   }} p->v =-1;
    At the end, the V is changed to 1 for the} int Findtrie (char *str) {int len = strlen (str);
	Trie *p = &root;
    if (P==null) return 0;
        for (int i=0; i<len; ++i) {int id = str[i]-' 0 ';
        p = p->next[id];
    if (p = = NULL)//If it is an empty set, the string that is prefixed by this is not saved        return 0;
		if (p->v = = -1&&i==len-1)//have the same string return-1;
	if (p->v = =-1)//The string in the character set is the prefix of this string return-1;   } return-1;
    This string is the prefix of a string in the character set} int Dealtrie (trie* T) {int i;
    if (T==null) return 0;
			for (i=0;i<max;i++) {if (t->next[i]!=null) {Dealtrie (t->next[i]);
		t->next[i]=null;
    }} if (T!=&root) free (T);
return 0;
} Char str[100]; 
	int main () {int t;
	scanf ("%d", &t);
		while (t--) {int n;
		scanf ("%d", &n); 
		int flag=1;
			while (n--) {scanf ("%s", str);
			int Tem=findtrie (str);
			if (tem==-1) flag=0; 
		Createtrie (str);
		} if (flag==0) puts ("NO"); 
		Else puts ("YES");
	Dealtrie (&root);
} return 0; }



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.