hdu 1671 phone list【Trie 树】

本文介绍了一种使用Trie树解决电话号码簿中号码是否互为前缀的问题的方法,并提供了详细的代码实现,包括如何插入号码、查找以及释放内存。

 

   Phone List

            Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
                  Total Submission(s): 3544    Accepted Submission(s): 1191


 

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

 

题意:所给的号码里面不能是其他号码的前缀,如果不满足这个条件,输出NO,否则YES;

思路:这是一道字典树也就是Trie树的模板题,没有A的估计都是超内存的,这里要注意的是,如果是多组数据输入,那么每一组输入输出完,要把用过的指针都释放掉,这样就不会超内存了,多加一个释放指针的函数就行了,主要是看如何释放内存,我释放内存的方法有点类似dfs,从root开始,遍历child有没有不是NULL,有就递归一次,返回的时候把指针释放掉;,下面看代码;

 

#include<iostream>
#include<cstdio>
#include<map>
#include<cstring>

using namespace std;

struct Trie {
    bool End;
    Trie *child[12];
    Trie () {
        memset(child,0,sizeof(child));
        End = false;
    }
};

void insert_ (Trie *root, char *str) {
    int tmp,len = strlen(str);
    Trie *rt = root;
    for (int i = 0; i < len; ++i) {
        tmp = str[i]-'0';
        if(rt->child[tmp] == NULL) {
            rt->child[tmp] = new Trie();
        }
            rt = rt->child[tmp];
    }
    rt->End = true;
}

bool search_ (Trie *root, char *str) {
    int tmp,len = strlen(str),cnt = 0;
    Trie *rt = root;
    for (int i = 0; i < len; ++i) {
        tmp = str[i]-'0';
        rt = rt->child[tmp];
        if(rt->End && i != len-1) return false; // != len-1是为了排除掉最后末尾是END = true的情况
    }
    return true;
}


void refree (Trie *rt) {

    for (int i = 0; i < 12; ++i) {
        if(rt->child[i] != NULL) refree(rt->child[i]);
        delete (rt->child[i]);
    }
}

char num[10002][13];
int main(void)
{
   // freopen("in.txt","r",stdin);
   // freopen("out.txt","w",stdout);
    int t,n;
    scanf("%d",&t);
    while (t--) {
        scanf("%d",&n);
        Trie *root = new Trie();
        memset(num,0,sizeof(num));
        for (int i = 0; i < n; ++i) {
            scanf(" %s",num[i]);
            insert_ (root, num[i]);
        }
        bool ok = true;
        for (int i = 0; i < n; ++i) {
            if(!search_ (root, num[i])) { ok = false; break; }
        }
        if(ok) printf("YES\n");
        else printf("NO\n");
        refree(root);
        delete (root);
    }
    return 0;
}

 

 

 

 

 

 

 

 

胚胎实例分割数据集 一、基础信息 • 数据集名称:胚胎实例分割数据集 • 图片数量: 训练集:219张图片 验证集:49张图片 测试集:58张图片 总计:326张图片 • 训练集:219张图片 • 验证集:49张图片 • 测试集:58张图片 • 总计:326张图片 • 分类类别: 胚胎(embryo):表示生物胚胎结构,适用于发育生物学研究。 • 胚胎(embryo):表示生物胚胎结构,适用于发育生物学研究。 • 标注格式:YOLO格式,包含实例分割的多边形标注,适用于实例分割任务。 • 数据格式:图片来源于相关研究领域,格式为常见图像格式,细节清晰。 二、适用场景 • 胚胎发育AI分析系统:构建能够自动分割胚胎实例的AI模型,用于生物学研究中的形态变化追踪和量化分析。 • 医学与生物研究:在生殖医学、遗传学等领域,辅助研究人员进行胚胎结构识别、分割和发育阶段评估。 • 学术与创新研究:支持计算机视觉与生物医学的交叉学科研究,推动AI在胚胎学中的应用,助力高水平论文发表。 • 教育与实践培训:用于高校或研究机构的实验教学,帮助学生和从业者掌握实例分割技术及胚胎学知识。 三、数据集优势 • 精准与专业性:实例分割标注由领域专家完成,确保胚胎轮廓的精确性,提升模型训练的可靠性。 • 任务专用性:专注于胚胎实例分割,填补相关领域数据空白,适用于细粒度视觉分析。 • 格式兼容性:采用YOLO标注格式,易于集成到主流深度学习框架中,简化模型开发与部署流程。 • 科学价值突出:为胚胎发育研究、生命科学创新提供关键数据资源,促进AI在生物学中的实际应用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值