hdu 1671&& poj 3630 (trie 树应用)
Phone List
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 25280 Accepted: 7678 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:
- Emergency 911
- Alice 97 625 999
- 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
98346Sample Output
NO
YESSource
为何感觉一用指针就各种力不从心?
题意:t组实例,每组n个号码,判断里面号码是否有谁是谁的前缀,如果有则输出”NO“,否则“YES”;
亮点:插入新单词时给每个分支的结束处加一个结束标志,那么便出现了两种情况1.之前单词顺着这个分支走还没结束,而新插入的单词到这里结束了,说明新单词与旧单词重叠,则说明冲突,2.新插入单词未结束,而旧单词在这个节点结束了,说明旧单词是新单词的前缀,判断直接结束,冲突了~
#include<iostream>
#include<vector>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <math.h>
#include<algorithm>
#define ll long long
#define eps 1e-8
using namespace std;
int p;
struct nodes
{
int cnt;
struct nodes *next[];
}* root,*temp,treenodes[];//静态申请节点比较省时间,也可以不用释放内存 int inserts(char word[])
{
nodes *cur = root;
int i = ;
while(word[i] )
{
int t = word[i] - '';
if(cur->next[t] )
{
if(cur->next[t]->cnt == || word[i + ] == '\0')//这里的结束条件最容易出错,关键也在这里
return ;
}
else
{
temp = &treenodes[p++];
temp->cnt = ;
for(int i = ; i < ; i++)
{
temp->next[i] = NULL;
}
cur->next[t] = temp;
}
cur = cur->next[t];
//printf("%d %c\n",cur->cnt,word[i]);
i++;
}
cur->cnt = ;
//printf("%d %c\n",cur->cnt,*word);
return ;
} int main(void)
{
int t,n,ans,last;
char phonenum[];
scanf("%d",&t);
while(t--)
{
p = ;
scanf("%d",&n);
root = &treenodes[p++];
root->cnt = ;
for(int i = ; i < ; i++)
{
root->next[i] = NULL;
}
last = ;
for(int i = ; i < n; i++)
{
scanf("%s",phonenum);
if(last)//如果只前的电话号码已经冲突,則不再插入新节点~
ans = inserts(phonenum);
if(ans == )
last = ;
}
if(last)
printf("YES\n");
else
printf("NO\n");
}
return ;
}
hdu 1671&& poj 3630 (trie 树应用)的更多相关文章
- POJ 3630 trie树
Phone List Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26559 Accepted: 8000 Descripti ...
- hdu 1671 Phone List (Trie树)
简单的字典树应用,在建树的时候判断就行了. 需要注意的语法: 在使用malloc和free来处理动态内存的时候,仅仅是释放了这个对象所占的内存,而不会调用这个对象的析构函数:使用new和delete就 ...
- hdu 1671 Phone List 字典树
// hdu 1671 Phone List 字典树 // // 题目大意: // // 有一些电话号码的字符串长度最多是10,问是否存在字符串是其它字符串的前缀 // // // 解题思路: // ...
- poj 2945 trie树统计字符串出现次数
用记录附加信息的val数组记录次数即可. trie的原理:每个可能出现的字目给一个编号c,那么整个树就是一个c叉树 ch[u][c]表示 节点u走c边过去之后的节点 PS:trie树还有种动态写法,使 ...
- HDU 4757 可持久化trie树
首先如果给定一些数,询问这些数中哪个数^给定的数的值最大的话,我们可以建立一颗trie树,根连接的两条边分别为0,1,表示二进制下第15位,那么我们可以建立一颗trie树,每一条从根到叶子节点的链表示 ...
- HDU - 1251 统计难题(trie树)
Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). Input输入数据的第一部 ...
- HDU 4760 Good Firewall ( Trie树 )
一开始看的时候就想歪了,比赛的时候一直在YY线段树区间覆盖,然后纠结节点数太多开不下怎么办啊啊啊啊…… 然后昨天吃饭的时候也在纠结这到底是个啥题,后来发现公共前缀->前缀??!!!!->这 ...
- POJ 2945 trie树
Find the Clones Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 7704 Accepted: 2879 Descr ...
- POJ 2513 trie树+并查集判断无向图的欧拉路
生无可恋 查RE查了一个多小时.. 原因是我N define的是250500 应该是500500!!!!!!!!! 身败名裂,已无颜面对众人.. 吐槽完了 我们来说思路... 思路: 判有向图能否形成 ...
随机推荐
- 02_jQuery对象初识(二)筛选器1
0. HTML对象和jQuery对象的区别: 1.jQuery对象转换成DOM对象,用索引取出具体的标签 2.DOM对象转换成jQuery对象,$(DOM对象) 注意:jQuery对象保存到变量的时候 ...
- springboot拦截器的拦截配置和添加多个拦截器
在spring2.0之前的版本大部分都采用extends WebMvcConfigurerAdapter,把拦截器配置成一个bean,具体的方法,我不细说,网上一大堆.而在spring2.0之后,这个 ...
- 2018-8-10-dotnet-从入门到放弃的-500-篇文章合集
title author date CreateTime categories dotnet 从入门到放弃的 500 篇文章合集 lindexi 2018-08-10 19:16:52 +0800 2 ...
- AlexNet模型
AlexNet模型 <ImageNet Classification with Deep Convolutional Neural Networks>阅读笔记 一直在使用AlexNet,本 ...
- Css if hack条件语法
Css if hack条件语法 <!--[if !IE]><!--> 除IE外都可识别 <!--<![endif]--><!--[if IE]> ...
- Apache SOLR and Carrot2集成
1.环境 下载软件 名称 地址 solr-integration-strategies-gh-pages.zip https://github.com/carrot2/solr-integration ...
- watch、tail联合使用
因为用了tmux,不想调整窗格大小,只想输出命令结果的最后几行,所以就想出了这个方法. watch.tail联合用法 watch 'echo "`nvidia-smi`" | ta ...
- 微信小程序——tab导航栏
wxml: <view class="tab"> <view class="tab-left" bindtap="tabFun&q ...
- Delphi 设计模式:《HeadFirst设计模式》---行为模式之责任链模式
模式解说 责任链模式是一种对象的行为模式,它将处理客户端请求的那些对象联成一条链,并沿着这条链传递请求,直到有一个对象处理它为止. 通常使用在以下场合 1 有多个对象可以处理一个请求,哪个对象处理该请 ...
- megacli在线raid构建详解(转载自用)
版权声明:本文为博主原创文章,未经博主允许不得转载,转载附上原文链接即可. https://blog.csdn.net/GX_1_11_real/article/details/83213959 ht ...