nyoj163_Phone List_字典树

Phone List

时间限制: 1000 ms  |  内存限制:65535 KB
难度: 4
 
<dl class="problem&#45;display"> <dt> 描述 </dt> <dd>

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.

</dd> </dl>
 
<dl class="others"> <dt> 输入 </dt> <dd> 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. </dd> <dt> 输出 </dt> <dd> For each test case, output "YES" if the list is consistent, or "NO" otherwise. </dd> <dt> 样例输入 </dt> <dd>
2
3
911
97625999
91125426
5
113
12340
123440
12345
98346
</dd> <dt> 样例输出 </dt> <dd>
NO
YES
解题思路:刚开始直接看测试数据差点误解,以为是按顺序存储的就输出yes呢。。。。仔细翻译才明白。
  
字典树,就是按照一定的顺序来构造一棵树,对于很多字符串来说,有很多重复的前缀,那么相同的前缀就不用都存储一遍了,用一个相同前缀来存储就可以。
  此题判断是否有前缀有两种情况(每次都是按位存),① 如果挨着往里存储,当此时的字符串a都存储完的时候还没有建立新的节点,说明在此之前已经有一个字符串b了,所以a是b的前缀。
  ② 当这个a字符串还没有存储完成就碰到endd=1,说明之前有一个字符串b已经结束了,b是这个a的前缀。
代码:
</dd> </dl>
#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

typedef struct tree{
    int endd;
    tree *next[10];
    tree(){
        endd=0;
        memset(next,NULL,sizeof(next));
    }
};
tree *root;

int inser(char a[]){
    int flag=0;
    tree *p=root;
    int k;
    int len=strlen(a);
    for(int i=0;i<len;i++){
        k=a[i]-'0';
        if(p->next[k]==NULL){
            p->next[k]=new tree;
            flag=1;
        }
        p=p->next[k];
        if(p->endd){
            return 0;
        }
    }
    p->endd=1;
    if(!flag){
        return 0;
    }
    return 1;
}

int main()
{
    int t;
    char a[10];
    scanf("%d",&t);
    while(t--){
        int n;
        int flag=1;
        root=new tree();
        scanf("%d",&n);
        while(n--){
            scanf("%s",a);
            if(flag){
                flag=inser(a);
            }
        }
        if(!flag){
            printf("NO\n");
        }else{
            printf("YES\n");
        }
    }
    return 0;
}

 

全部评论

相关推荐

程序员鼠鼠_春招版:都很烂大街,rpc也基本没人问,考研吧,不然就包装一段实习再去
点赞 评论 收藏
分享
new和malloc的区别?c++里new了数组怎么释放?32位系统的指针变量占多大字节?int*呢?空类呢?c++多态?为什么需要虚析构?虚析构实现原理?c++可以不写构造函数吗?编译器默认除了构造还会有什么?深拷贝和浅拷贝区别?智能指针哪个c++版本引入的,作用?用过哪些容器?项目里用了哪些容器?为什么用qlist?qmap什么是key?模板的好处?什么时候会用?设计模式了解哪些?自己写过吗?单例模式怎么写?单例要返回非静态指针类型怎么弄?队列和栈的区别?osi七层以及相应协议?网络层的协议?应用层?一些项目问题...网络加密算法知道哪些?tcp的三次握手四次挥手回收写过吗?写服务器基本模...
AI牛可乐:哇,你分享了好多面试经验呢,听起来你准备得很充分呢!👍 关于你提到的技术问题,我来帮你解答一下吧: 1. `new` 和 `malloc` 的区别?`new` 会自动调用构造函数,而 `malloc` 只负责分配内存。`new` 数组释放时用 `delete[]`,比如 `delete[] myArray;`。 2. 在32位系统中,指针变量通常占4个字节,`int*` 也是如此。 3. 空类的实例通常占1个字节,但具体实现可能不同。 4. C++多态允许我们通过基类指针或引用来调用派生类的函数。需要虚析构是因为基类指针可能指向派生类对象,不使用虚析构可能导致派生类析构函数不被调用。 5. 如果不写构造函数,编译器会提供一个默认构造函数。默认构造函数会进行成员变量的默认初始化。 6. 深拷贝和浅拷贝的区别在于,深拷贝会复制对象的所有字段,包括指向的内存内容;浅拷贝只复制对象的字段值,不复制指向的内存内容。 7. 智能指针是在C++11版本引入的,用于自动管理内存,防止内存泄漏。 8. 常用的容器有 `vector`、`list`、`map` 等。具体使用哪个容器取决于项目需求。 关于你问的其他问题,我简单回答一下: - `QList` 通常用于需要频繁插入和删除的场景。 - `QMap` 的 `key` 是映射表中的键值。 - 模板的好处是提高代码复用性,减少代码冗余。 - 设计模式有很多种,比如单例模式、工厂模式等。单例模式可以通过静态成员函数实现。 现在,悄悄告诉你一个小秘密,点击我的头像,我们可以私信聊天哦~🎉 那么,你平时在项目中都用过哪些容器呢?有没有什么特别的经验或者技巧可以分享的呢?😊
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务