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. 

InputThe 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.OutputFor 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 这个题目注意下要释放内存,同时他不是按顺序的。在后面也会出现有前面单词前缀的单词。 借这个题目贴下字典树的模板把
#include<iostream>
#include<string>
#include<vector>
#include<sstream>
#include<algorithm>
using namespace std;
typedef long long ll;
struct Tire{
int num;
bool flag;
Tire *next[];
Tire() {
num = ;
flag = false;
for(int i=;i<;i++){
next[i] = NULL;
}
}
};
Tire *tree;
bool flag; //判断字典树中是否有单词是别人的前缀
ll ans = ; //记录不同的单词总数
void Insert(string word){ //在字典树中插入word字符串
Tire* p = tree;
for(int i=;i<word.length();i++){
int t = word[i] - '';
if( p -> next[t] == NULL ){
p -> next[t] = new Tire;
}
p = p -> next[t];
if( p -> flag ) {
flag = false;
}
p -> num ++;
}
if( !p->flag ) {
p -> flag = true;
ans ++;
}
if( p -> flag ) {
if( p -> num > ) {
flag = false;
}
}
}
int Find(string word){ //计算当前前缀出现次数
Tire* p = tree;
for(int i=;i<word.length();i++){
int t = word[i] - 'a';
if( p -> next[t] == NULL ) {
return ;
}
p = p -> next[t];
}
return p -> num;
}
bool isLife(string word){//判断当前字符串在字典树中是否存在
Tire* p = tree;
for(int i=;i<word.length();i++){
int t = word[i] - 'a';
if( p -> next[t] == NULL ) {
return false;
}
p = p -> next[t];
}
if( p -> flag == true ){
return true;
}
return false;
}
int deal(Tire* T)//释放内存空间
{
int i;
for(i=;i<=;i++)
{
if(T->next[i]!=NULL)
deal(T->next[i]);
}
free(T);
return ;
}
int main(){
int T;
cin >> T;
while( T -- ) {
int n;
cin >> n;
flag = true;
tree = new Tire;
while( n -- ) {
string s;
cin >> s;
Insert(s);
}
if( flag ) {
cout << "YES" << endl;
}else cout << "NO" << endl;
deal(tree);
}
return ;
}

hdu 1671 Phone List 字典树模板的更多相关文章

  1. hdu 1671 Phone List 字典树

    // hdu 1671 Phone List 字典树 // // 题目大意: // // 有一些电话号码的字符串长度最多是10,问是否存在字符串是其它字符串的前缀 // // // 解题思路: // ...

  2. [ACM] hdu 1671 Phone List (字典树)

    Phone List Problem Description Given a list of phone numbers, determine if it is consistent in the s ...

  3. 字典树模板题(统计难题 HDU - 1251)

    https://vjudge.net/problem/HDU-1251 标准的字典树模板题: 也注意一下输入方法: #include<iostream> #include<cstdi ...

  4. HDU - 1251 字典树模板题

    Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).  Input输入数据的第一部 ...

  5. 字典树模板 HDU - 1251

    题意: 给一些单词,换行键后,查找以后输入的单词作为前缀的话们在之前出现过几次. 思路: 字典树模板----像查字典的顺序一样 #include<string> #include<s ...

  6. hdu1521(字典树模板)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意: 中文题诶~ 思路: 字典树模板 代码1: 动态内存, 比较好理解一点, 不过速度略慢, ...

  7. hdu 1251 统计难题 (字典树(Trie)<PS:C++提交不得爆内存>)

    统计难题Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submis ...

  8. CH 1601 - 前缀统计 - [字典树模板题]

    题目链接:传送门 描述给定 $N$ 个字符串 $S_1,S_2,\cdots,S_N$,接下来进行 $M$ 次询问,每次询问给定一个字符串 $T$,求 $S_1 \sim S_N$ 中有多少个字符串是 ...

  9. HDU 2072 - 单词数 - [(有点小坑的)字典树模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2072 Problem Descriptionlily的好朋友xiaoou333最近很空,他想了一件没有 ...

随机推荐

  1. S2:c#继承

    在C#中,如果一个类后面通过冒号又跟了另外一个类,那么我们就称冒号前面的类为子类,冒号后面的类为父类.这种书写类的方式放映出来的关系就称为类的继承关系. 1.子类:派生类 父类:基类或者超类 满足is ...

  2. Wpf窗口设置屏幕居中最前显示

    public Window()         {             InitializeComponent();             WindowStartupLocation = Win ...

  3. luogu2279_[HNOI2003]消防局的设立 贪心

    传送门 不需要树形dp 关于深度排序 当前节点到最近的消防局(f[u])>2时要建新的与u的上面(v)的上面(w) 同时w的上面和上面的上面也要更新f值 #include <bits/st ...

  4. React 如何搭建脚手架

    React 如何搭建脚手架   npm install -g create-react-app    //安装 create-react-app react-demo    // react-demo ...

  5. ThreadPoolExecutor线程池的一个面试题

    问题:现有一个线程池,参数corePoolSize = 5,maximumPoolSize = 10,BlockingQueue阻塞队列长度为5,此时有4个任务同时进来,问:线程池会创建几条线程? 如 ...

  6. 如何为 caddy 添写自定义插件

    如何为 caddy 添写自定义插件 项目地址:https://github.com/yhyddr/quicksilver/tree/master/gosample/caddy-plugin 前言 Ca ...

  7. 【Kubernetes 系列五】在 AWS 中使用 Kubernetes:EKS

    目录 1. 概述 2. 版本 3. 预备 3.1. 操作环境 3.2. 角色权限 3.2.1. CloudFormation 完全权限 3.2.2. EKS 读写权限 3.2.3. EC2 相关权限 ...

  8. windows查看端口被占用

    1.打开控制台终端 2.在命令行下输入netstat -ano|findstr "8080"(8080是被占用的端口) 3.记住最后一列的数字PID如4684 4.输入taskli ...

  9. XAMPP/LAMPP到底在哪里启用APACHE2的rewrite

    XAMPP/LAMPP是一套我们在个人建站过程中非常便捷常用的集成环境.特别是对于学习PHP开发和建站非常便捷. 最近在使用CentOS7环境下的XAMPP过程中,遇到了一个问题,也就是apache2 ...

  10. (六)c#Winform自定义控件-单选框

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...