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最近很空,他想了一件没有 ...
随机推荐
- jenkins弱口令漏洞
jenkins弱口令漏洞 一.漏洞描述 通过暴力破解管理控制台,如果爆破成功,可获得后台管理权限.操作后台,后台可通过脚本命令行功能执行系统命令,如反弹shell等,低权限可以通过创建控制台输出方式执 ...
- 使用request获取访问者的真实IP
在JSP里,获取客户端的IP地址的方法是:request.getRemoteAddr(),这种方法在大部分情况下都是有效的.但是在通过了Apache,Squid等反向代理软件就不能获取到客户端的真实I ...
- java中System.out.println()打印输出结果
疑点:syso()是打印输出语句,打印的是什么? syso()不同情况下打印输出的结果不一样: 1. package com.briup; public class Syso { public sta ...
- plotly之set_credentials_file问题
相信了解可视化的同学们都听说过plotly,笔者也是第一次了解这个网站,然后兴冲冲地设置,但是没想到第一次进行在线账号初始化就出现了问题! python3报错为module 'plotly.tools ...
- WPF中如何禁用空格键(或其他键)
在选择的控件中添加KeyDown event method private void OnKeyDown(object sender, KeyEventArgs e){ if (e.Key == Ke ...
- 夯实Java基础(三)——面向对象之继承
1.继承概述 继承是Java面向对象的三大特征之一,是比较重要的一部分,与后面的多态有着直接的关系.继承就是子类继承父类的特征和行为,使得子类对象(实例)具有父类的实例域和方法,或子类从父类继承方法, ...
- Install eclipse ns3 in ubuntu 14.04
1. NS3 install 参考NS3 tutorial即可. 2.eclipse 2.1下载 下载地址:http://www.eclipse.org/downloads/ ...
- 什么是HTML,HTML的简介,HTML结构
html:超文本标记语言(Hyper Text Markup Language) ==============基本结构================= <html><!--最外层为 ...
- 压力测试-jmeter
1. 场景描述 新申请的服务器,要压测下python算法程序最多能执行多少条数据,有几年没用压力测试工具-jmeter了,重新下载了最新版本,记录下,也希望能帮到准备使用jmeter做压测的朋友. 2 ...
- Yii CGridView 之 SQL 语句
在CGridView里,有时候需要用到复杂的查询时,可用 CSqlDataProvider替换CActiveDataProvider, CSqlDataProvider 可用复杂的查询语句,例子如下: ...