hdu1671 Phone List 字典树(小组赛E)

链接: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): 18366    Accepted Submission(s): 6181


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

Input
The first line of input gives a single integer, 1 <= t <= 40, the number of test cases. Each test case starts with n, the number of phone numbers, on a separate line, 1 <= n <= 10000. Then follows n lines with one unique phone number on each line. A phone number is a sequence of at 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
题意:就是判断输入的电话号码中是否有号码是其他号码的前缀

思路:字典树

基本的操作
1.建立:把母字符串每个字符串存入树,如存入ab、abc、df、de,如图
2.查找
3.插入
4.删除

搜索字典项目的方法为:
(1) 从根结点开始一次搜索;
(2) 取得要查找关键词的第一个字母,并根据该字母选择对应的子树并转到该子树继续进行检索;
(3) 在相应的子树上,取得要查找关键词的第二个字母,并进一步选择对应的子树进行检索。
(4) 迭代过程……
(5) 在某个结点处,关键词的所有字母已被取出,则读取附在该结点上的信息,即完成查找。
其他操作类似处理

节点基本结构

建立

查找


代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node
{
    node *next[10];
    int v;
    int f;
}*tree,t;
tree root;
int flag;
void insert(char *str)
{
    tree p=root,newnode;
    for(;*str!='\0';str++)
    {
        if(p->next[*str-'0']!=NULL)
        {   
            p->f=1;
            p=p->next[*str-'0'];
            if(p->v==-1)//前面的串中出现过当前串的前缀
                flag=1;
        }
     else
      {
        newnode=(tree)malloc(sizeof(t));
        for(int i=0;i<=9;i++)
            newnode->next[i]=NULL;
        newnode->v=0;
        newnode->f=0;
        p->f=1;
        p->next[*str-'0']=newnode;
        p=newnode;
         
        }   
   }
    p->v=-1;
    if(p->f)//当前串是之前出现过的串的前缀
    {
        flag=1;
    }
}
int deal(node* T)//释放内存空间
{
    int i;
    for(i=0;i<=9;i++)
    {
        if(T->next[i]!=NULL)
            deal(T->next[i]);
    }
    free(T);
    return 0;
}
int main()
{
    int m,n,num;
    char str[15];
    scanf("%d",&m);
    while(m--)
    {
        flag=0;
        root=(tree)malloc(sizeof(t));
        for(int i=0;i<=9;i++)
            root->next[i]=NULL;
        root->v=0;
        root->f=0;
        scanf("%d",&n);getchar();
        while(n--)
        {
            gets(str);
            if(flag)//算是优化吧,可是 还是很慢呀,差距,差距啊
                continue;
            insert(str);
            //puts(str);
        }
        if(!flag)
            printf("YES\n");
        else printf("NO\n");
        deal(root);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值