hdu 1671 Phone List 字典树模板
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 字典树模板的更多相关文章
- hdu 1671 Phone List 字典树
// hdu 1671 Phone List 字典树 // // 题目大意: // // 有一些电话号码的字符串长度最多是10,问是否存在字符串是其它字符串的前缀 // // // 解题思路: // ...
- [ACM] hdu 1671 Phone List (字典树)
Phone List Problem Description Given a list of phone numbers, determine if it is consistent in the s ...
- 字典树模板题(统计难题 HDU - 1251)
https://vjudge.net/problem/HDU-1251 标准的字典树模板题: 也注意一下输入方法: #include<iostream> #include<cstdi ...
- HDU - 1251 字典树模板题
Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). Input输入数据的第一部 ...
- 字典树模板 HDU - 1251
题意: 给一些单词,换行键后,查找以后输入的单词作为前缀的话们在之前出现过几次. 思路: 字典树模板----像查字典的顺序一样 #include<string> #include<s ...
- hdu1521(字典树模板)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意: 中文题诶~ 思路: 字典树模板 代码1: 动态内存, 比较好理解一点, 不过速度略慢, ...
- hdu 1251 统计难题 (字典树(Trie)<PS:C++提交不得爆内存>)
统计难题Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submis ...
- CH 1601 - 前缀统计 - [字典树模板题]
题目链接:传送门 描述给定 $N$ 个字符串 $S_1,S_2,\cdots,S_N$,接下来进行 $M$ 次询问,每次询问给定一个字符串 $T$,求 $S_1 \sim S_N$ 中有多少个字符串是 ...
- HDU 2072 - 单词数 - [(有点小坑的)字典树模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2072 Problem Descriptionlily的好朋友xiaoou333最近很空,他想了一件没有 ...
随机推荐
- Java集合系列(四):HashMap、Hashtable、LinkedHashMap、TreeMap的使用方法及区别
本篇博客主要讲解Map接口的4个实现类HashMap.Hashtable.LinkedHashMap.TreeMap的使用方法以及三者之间的区别. 注意:本文中代码使用的JDK版本为1.8.0_191 ...
- 从无到满意offer,你需要知道的那些事
本文首发于微信公众号:[坂本先生] 原文地址:从无到满意offer,你需要知道的那些事 1.求职软件/网站汇总 软件 评价 推荐指数 拉钩网 手机端产品设计的比较好,当时在上面找到了很多的面试机会 5 ...
- ASP.NET Core on K8S深入学习(3)Deployment
上一篇<部署过程解析与安装Dashboard>中我们了解K8S的部署过程,这一篇我们来了解一下K8S为我们提供的几种应用运行方式:Deployment.DaemonSet与Job,它们是K ...
- 洛谷P1510 题解
前言: 其实这道题挺水的,但我居然把ta想成了 贪心 啪啪打脸 好了,废话不多说. 思路: step 1:先翻译以下题意,其实就是求出最多消耗多少体力能把东海填满,如果不能填满,就输出"Im ...
- 【精选】Markdown 语法汇总
博客园也能Markdown?美滋滋,Markdown真的是好用QAQ. 本文档按照Markdown各种常用语法类别,以文字描述+演示的方式来展现markdown语法的使用.Markdown 的目标是实 ...
- 9、数组中删除元素(test6.java)
前文讲到,通过函数,进行数组元素的添加,这里同样通过这个函数,进行数组的删除. 举个例子,代码如下: //导入输入所需要的包 import java.util.Scanner; public clas ...
- 在vue-cli 3中, 给stylus、sass样式传入共享的全局变量
在开发中有时,我们定义了大量的基础样式变量,例如: 大量的vue单文件组件会用到这些变量,每个组件都引人一次又太麻烦.全局引入是个不错的方法,于是,在main.js 中引入variable.styl文 ...
- 什么是Singleton?
Singleton:在Java中即指单例设计模式,它是软件开发中最常用的设计模式之一. 单:指唯一 例:指实例 单例设计模式,即某个类在整个系统中只能有一个实例对象可被获取和使用的代码模式. 要点: ...
- powerdesign进军(二)--oracle数据源配置
目录 资源下载(oracle客户端) 配置 查看系统的数据源 powerdesign 连接数据库 title: powerdesign进军(二)--oracle数据源配置 date: 2019-05- ...
- 洛谷 P3195 [HNOI2008]玩具装箱TOY
题意简述 有n个物体,第i个长度为ci 将n个物体分为若干组,每组必须连续 如果把i到j的物品分到一组,则该组长度为 \( j - i + \sum\limits_{k = i}^{j}ck \) 求 ...