惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 三生石上(FineUI控件)
G
Google Developers Blog
Vercel News
Vercel News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
N
Netflix TechBlog - Medium
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Engineering at Meta
Engineering at Meta
B
Blog
博客园_首页
量子位
博客园 - 叶小钗
L
LangChain Blog
T
The Blog of Author Tim Ferriss
云风的 BLOG
云风的 BLOG
Blog — PlanetScale
Blog — PlanetScale
F
Fortinet All Blogs
S
SegmentFault 最新的问题
宝玉的分享
宝玉的分享
D
DataBreaches.Net
雷峰网
雷峰网
The Cloudflare Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Last Week in AI
Last Week in AI
P
Proofpoint News Feed
TaoSecurity Blog
TaoSecurity Blog
罗磊的独立博客
MongoDB | Blog
MongoDB | Blog
The GitHub Blog
The GitHub Blog
I
Intezer
H
Help Net Security
The Hacker News
The Hacker News
The Register - Security
The Register - Security
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
AWS News Blog
AWS News Blog
V
V2EX
Microsoft Security Blog
Microsoft Security Blog
T
Tenable Blog
Spread Privacy
Spread Privacy
A
Arctic Wolf
P
Proofpoint News Feed
T
Threat Research - Cisco Blogs
Schneier on Security
Schneier on Security
C
CERT Recently Published Vulnerability Notes
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
The Last Watchdog
The Last Watchdog
Latest news
Latest news
T
Troy Hunt's Blog
L
LINUX DO - 热门话题

Lss233's.Blog()

10 分钟快速入门垃圾回收机制 2022: 艰难,但充满希望 MiniDB 开发手札2 - 网络通信: PostgreSQL 服务端实现 MiniDB 开发手札1 - 概览 HELLO: 2022 OpenVINO + YoloV5 目标视觉炼丹流程简述 使用 Tinc 组建虚拟内网,并接入 DN42 TOJ 1175 - 线段树模板题 HDU 1257 - 统计难题 于是乎,我就这样活过了2020:这是一篇年末总结 这个Lss233一事无成却敢写年末总结:2019,再见啦。 噩梦24小时:记一次服务器迁移与宕机过程 新技能学习:教你如何阅读Java字节码 让我们用PGP进行安全地交流吧!
HDU 1671 - Phone List
Lss233 · 2021-01-07 · via Lss233's.Blog()

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

题解

题目会输入一组数字。要求任意一组数字不得是另外一组的前缀。
比如说:

91125426
911

或者

812345
8123456

这样的话只需要线段树在插入的时候,判断经过的节点是否被标记为 end
或者在插入结束后,判断最后一个节点是否为第一次被插入就好了。

另外,本题的内存限制比较小,
所以在一组数据的结束,需要释放内存(就算你不说我也会这么做的,因为一个 delete 清空字典树超方便呀)。

#include <bits/stdc++.h>
const int MAX_CHILDREN = 11;
struct node
{
    bool end;
    node *children[MAX_CHILDREN];
    int count = 0;
    ~node() {
        for (int i = MAX_CHILDREN - 1; i >= 0; i--)
        {
            if(children[i]) {
                delete children[i];
            }
        }
    }
    node()
    {
        end = false;
        for (int i = 0; i < MAX_CHILDREN; i++)
            children[i] = NULL;
    }
};
node *root;
bool yes = true;
/**
 * 插入一个新的字符串
 * @param str 字符串数组
 * @param len 字符串长度
 **/
void insert(char *str, int len)
{
    node *location = root;
    for (int i = 0; i < len; i++)
    {
        if (!yes)
            return; // Thereisnoneed.
        if (str[i] == 0 || str[i] == '\n')
            continue;
        int id = str[i] - '0';
        if (location->children[id] == NULL)
        {
            location->children[id] = new node;
        }
        if (location->end)
        {
            yes = false;
            return;
        }
        location = location->children[id];
        location->count++;
    }
    location->end = true;
    if (yes)
    {
        yes = location->count == 1;
    }
}
char str[20];
int main()
{
    int T;
    scanf("%d", &T);
    while (T--)
    {
        root = new node;
        yes = true;
        int n;
        scanf("%d", &n);
        while (n--)
        {
            scanf("%s", str);
            int len = strlen(str);
            insert(str, len);
        }
        printf("%s\n", yes ? "YES" : "NO");
        delete root;
    }
}