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. bit、byte、kb、mb、g的区别

    1Byte=8bit1KB=1024Byte(字节)=8*1024bit1MB=1024KB1GB=1024MB1TB=1024GB bit是计算机数据的最小单元.要么是0,要么是1. byte 关键 ...

  2. git开发流程

    典型的工作流程和做法是,由于你没有远程仓库的权限,你先在github通过fork,复制自己的一份远程仓库,然后通过clone你自己这个远程副本到本地,进行修改,修改后push到自己的githu远程副本 ...

  3. selenium操作cookies实现免密登录,自动发微博

    一直想用selenium实现个小功能,比如发微博之类的,但是有的网站在登录会有验证码,没想到太好的方法解决,于是想到利用cookies来登录网站 第一步:获取一个可用的cookies,获取的cooki ...

  4. 使用 OpenSSL为WindowsServer远程桌面(RDP)创建自签名证书 (Self-signed SSL certificate)

    前言 笔者查阅很多资料,才写成此文章,如有错误,请读者们及时提出. 一般大家使用远程桌面(Remote Desktop)连接Windows Server时,总会有一个警告提示,如图1 图1 出现此警告 ...

  5. tp5css和js引入问题

    由于以前用的是tp3.2,现在转用tp5开啊个人博客,在引入CSS和JS的时候遇到了一些坑 在3.2时期需要在路径中添加public,而在tp5中则直接引入static即可. 在config.php下 ...

  6. JavaScript循环出现的问题——用闭包来解决

    在for循环中,数组长度为3,我本来是想对每个循环的元素绑定一个点击事件的,结果点击后控制台输出全部为1. for (var i = 0; i < data.data.length; i++) ...

  7. 重读《学习JavaScript数据结构与算法-第三版》- 第3章 数组(一)

    定场诗 大将生来胆气豪,腰横秋水雁翎刀. 风吹鼍鼓山河动,电闪旌旗日月高. 天上麒麟原有种,穴中蝼蚁岂能逃. 太平待诏归来日,朕与先生解战袍. 此处应该有掌声... 前言 读<学习JavaScr ...

  8. 常见ASP脚本攻击及防范技巧

    由于ASP的方便易用,越来越多的网站后台程序都使用ASP脚本语言.但是, 由于ASP本身存在一些安全漏洞,稍不小心就会给黑客提供可乘之机.事实上,安全不仅是网管的事,编程人员也必须在某些安全细节上注意 ...

  9. 开源音乐下载神器XMusicDownloader更新,支持歌单一键下载,支持无损音乐

    开源音乐下载神器XMusicDownloader更新啦,新增网易.腾讯音乐歌单歌曲.歌手歌曲.专辑歌曲一键下载,同时支持下载flac无损音乐. 功能 V1.0 功能开源工具软件XMusicDownlo ...

  10. forward(转发)和redirect(重定向)的区别

    在学习Servlet和JSP时,经常会使用到forward和redirect,我们先来看这两者在Servlet中的调用方式: 1.forward request.getRequestDispatche ...