Phone number [trie tree]

Source: Internet
Author: User

Phone number viewing statistics submitted question total time limit: 1000 ms memory limit: 65536kb description
For some phone numbers, check whether they are consistent, that is, whether a phone number is the prefix of another phone number. For example:


Emergency 911
Alice 97, 625, 999
Bob 91 12 54 26


In this example, we cannot call Bob because the emergency phone is its prefix.


Emergency will be connected first, so these phone numbers are not consistent.


Input
The first line is an integer T, 1 ≤ T ≤ 40, indicating the number of test data.
The first line of each test example is an integer N, 1 ≤ n ≤ 10000, And the next n rows are a phone number of no more than 10 digits.


Output
For each test data, if the output is consistent with "yes", if the output is not "no ".


Sample Input
2
3
911
97625999
91125426
5
113
12340
123440
12345
98346
Sample output
No
Yes

Simple trie tree. Note two cases: Insert a short string before inserting a long string, and insert a long string before inserting a short string.

#include <stdio.h>#include <string.h>#include <stdlib.h>struct Node{    struct Node *next[10];    int isCover, isEnd;};int ok;void clean(Node *p){    memset(p->next, 0, sizeof(p->next));    p->isCover = p->isEnd = 0;}void insert(char *str, Node *root){    Node *p = root;    int id;    while(*str){        id = *str - '0';        if(p->next[id] == NULL){            p->next[id] = (Node *)malloc(sizeof(Node));            clean(p->next[id]);        }        p = p->next[id];        if(p->isEnd) ok = 0;        ++p->isCover;        ++str;    }    if(p->isCover > 1) ok = 0;    p->isEnd = 1;}void DELETE(Node *p){    for(int i = 0; i < 10; ++i)        if(p->next[i]) DELETE(p->next[i]);    free(p);}int main(){    int t, n;    char str[12];    scanf("%d", &t);    while(t--){        Node *root = (Node *)malloc(sizeof(Node));        scanf("%d", &n);        clean(root); ok = 1;        while(n--){            scanf("%s", str);            if(ok) insert(str, root);        }        printf(ok ? "YES\n" : "NO\n");        DELETE(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.