Phone List

Phone List

时间限制:1000 ms  |  内存限制:65535 KB
难度:4
描述

Given a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let's say the phone catalogue listed these numbers:

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

In this case, it's not possible to call Bob, because the central would direct your call to the emergency line as soon as you had dialled the first three digits of Bob's phone number. So this list would not be consistent.

输入
The first line of input gives a single integer, 1 ≤ t ≤ 10, the number of test cases. Each test case starts with n, the number of phone numbers, on a separate line, 1 ≤ n ≤ 100000. Then follows n lines with one unique phone number on each line. A phone number is a sequence of at most ten digits.
输出
For each test case, output "YES" if the list is consistent, or "NO" otherwise.
样例输入
2
3
911
97625999
91125426
5
113
12340
123440
12345
98346
样例输出
NO
YES



解题报告:用字典树


code:

#include
#include
#include
#include
#include
#include
#include

using namespace std;

typedef long long ll;
const int maxn=100005;
const int MAX=10;

typedef struct node
{
    struct node *next[MAX];
    int flag;  //标记是否是一个单词
}Trie;
Trie *root;
/*
root要初始化
root=(Trie *)malloc(sizeof(Trie));
root->flag=0;
for(int i=0;inext[i]=NULL;
}
*/

int createTrie(char *str) //创建一棵字典树,与查找合并
{
    int len = strlen(str);
    Trie *p = root, *q;
    for(int i=0; iflag==1) //查找1;说明已有一个单词作为前缀,比如119,119895
            return 1;
        int id = str[i]-'0'; //数字字符
        if(p->next[id] == NULL)
        {
            q = (Trie *)malloc(sizeof(Trie));
            q->flag = 0;    //初始v==1
            for(int j=0; jnext[j] = NULL;
            p->next[id] = q;
        }
        p = p->next[id];
    }
    for(int i=0;inext[i]!=NULL)
            return 1;
    }
    p->flag=1; //一个单词
    return 0;
}

void dealTrie(Trie* T) //清理内存root
{
    for(int i=0;inext[i]!=NULL)
            dealTrie(T->next[i]);
    }
    free(T);
}

int main()
{
  //  freopen("input.txt","r",stdin);
    int t,n;
    scanf("%d",&t);
    while(t--){
        root=(Trie *)malloc(sizeof(Trie)); //初始化
        root->flag=0;
        for(int i=0;inext[i]=NULL;
        }
        scanf("%d",&n);
        int flag=1; //默认YES
        char s[15];
        for(int i=0;i

你可能感兴趣的:(Phone,List,nyoj163)