hdu1671 Phone List [字典树 hash]
Phone List
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11633 Accepted Submission(s): 3965
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.
3
911
97625999
91125426
5
113
12340
123440
12345
98346
YES
| 12942795 | 2015-02-13 09:56:42 | Accepted | 1671 | 249MS | 4896K | 2261 B | G++ | czy |
| 12940774 | 2015-02-12 19:16:57 | Accepted | 1671 | 780MS | 3940K | 1566 B | G++ | czy |
自己写的很丑的字典树:
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<string> #define N 100005
#define M 205
#define mod 10000007
//#define p 10000007
#define mod2 1000000000
#define ll long long
#define LL long long
#define eps 1e-6
#define inf 100000000
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int n;
int T;
int flag; typedef struct
{
int v;
vector<int>nt;
}PP; PP p[N];
int cnt[N]; int tot;
vector<int>::iterator it; void insert(char s[])
{
int l=strlen(s);
int i;
int ff;
int st=;
int y;
for(i=;i<l;i++){
ff=;
for(it=p[st].nt.begin();it!=p[st].nt.end();it++){
y=*it;
if(p[y].v==s[i]-''){
ff=;
st=y;
break;
}
}
if(ff==){
p[st].nt.push_back(tot);
p[tot].v=s[i]-'';
p[tot].nt.clear();
st=tot;
tot++;
}
}
cnt[st]++;
} void ini()
{
memset(cnt,,sizeof(cnt));
flag=;
p[].nt.clear();
p[].v=-;
tot=;
scanf("%d",&n);
char s[];
while(n--){
scanf("%s",s);
insert(s);
}
} int check()
{
int st=;
queue<int>q;
q.push(st);
int te;
int y;
while(q.size()>=)
{
te=q.front();
q.pop();
if(p[te].nt.size()>= && cnt[te]>=){
return ;
}
for(it=p[te].nt.begin();it!=p[te].nt.end();it++){
y=*it;
q.push(y);
}
}
return ;
} void solve()
{
flag=check();
} void out()
{
if(flag==){
printf("NO\n");
}
else{
printf("YES\n");
}
} int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
scanf("%d",&T);
//for(int ccnt=1;ccnt<=T;ccnt++)
while(T--)
//scanf("%d%d",&n,&m);
//while(scanf("%d",&n)!=EOF)
{
ini();
solve();
out();
}
return ;
}
hash:
另一种解法:转自:http://fszxwfy.blog.163.com/blog/static/44019308201282484625551/
大致题意:多组电话号码,如果存在某组是其他组的前缀则输出NO
看了解题报告都说用字典树做,个人看了下题目数据突然想到一种巧法。
首先将所有的号码当做 long long 型存入数组,读完后进行排序,再从小到大读出每个数,并用map进行标记以示这个数出现过,然后在对这个数不断尽心除10操作,判断除10后的数有没有被标记,bingo..
不过WA了,后来想想看应该是有些号码前面是0,这样就不行了,不过后来一想可以将所有的数前面都加个1不就解决了。。。。
然后然后就这样ac了。。。。。 同学也在做 他都受不了 哈哈哈哈
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<string> #define N 20005
#define M 205
#define mod 10000007
//#define p 10000007
#define mod2 1000000000
#define ll long long
#define LL long long
#define eps 1e-6
#define inf 100000000
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int n;
int T;
map<ll,bool>mp;
int flag; void ini()
{
flag=;
scanf("%d",&n);
mp.clear();
char s[];
int l;
int i;
queue<ll>q;
ll tt=;
while(n--){
scanf("%s",s);
l=strlen(s);
tt=;
for(i=;i<l;i++){
tt=tt*+s[i]-'';
}
q.push(tt);
mp[tt]=true;
}
ll te;
while(q.size()>=){
te=q.front();
q.pop();
while(te>){
te=te/;
if(mp[te]==true){
flag=;return;
}
}
//q.push(nt);
} } void solve()
{ } void out()
{
if(flag==){
printf("NO\n");
}
else{
printf("YES\n");
}
} int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
scanf("%d",&T);
//for(int ccnt=1;ccnt<=T;ccnt++)
while(T--)
//scanf("%d%d",&n,&m);
//while(scanf("%d",&n)!=EOF)
{
ini();
solve();
out();
}
return ;
}
hdu1671 Phone List [字典树 hash]的更多相关文章
- Colored Sticks POJ - 2513 并查集+欧拉通路+字典树hash
题意:给出很多很多很多很多个棒子 左右各有颜色(给出的是单词) 相同颜色的可以接在一起,问是否存在一种 方法可以使得所以棒子连在一起 思路:就是一个判欧拉通路的题目,欧拉通路存在:没奇度顶点 或者 ...
- HDU1671 水题字典树
#include<cstdio> #include<cstdlib> #include<iostream> #include<cstring> #inc ...
- hihoCoder 1014trie树(字典树)
hihoCoder 1014 题目提示已经很清楚了~ 贴代码…… #include <iostream> #include <cstdio> #include <cstr ...
- POJ2513Colored Sticks(欧拉通路)(字典树)(并查集)
Colored Sticks Time Limit: 5000MS Memory ...
- poj 2503 Babelfish(Map、Hash、字典树)
题目链接:http://poj.org/bbs?problem_id=2503 思路分析: 题目数据数据量为10^5, 为查找问题,使用Hash或Map等查找树可以解决,也可以使用字典树查找. 代码( ...
- 字符串hash与字典树
title: 字符串hash与字典树 date: 2018-08-01 22:05:29 tags: acm 算法 字符串 概述 这篇主要是关于字符串里的 字符串hash 和 字符串字典树,,两个都是 ...
- hash(2018年CSUST省赛选拔赛第一场B题+hash+字典树)
题目链接:http://csustacm.com:4803/problem/1006 题目: 思路:正如题目一样,本题是一个hash,比赛的时候用的字典树,但是不知道为什么一直RE(听学长说要动态开点 ...
- POJ 2503 Babelfish(map,字典树,快排+二分,hash)
题意:先构造一个词典,然后输入外文单词,输出相应的英语单词. 这道题有4种方法可以做: 1.map 2.字典树 3.快排+二分 4.hash表 参考博客:[解题报告]POJ_2503 字典树,MAP ...
- day 1 堆 hash 线段树 树状数组 冰茶姬 字典树 二叉查找树
来郑州的第二天,早上开始也没说什么就说了些注意安全,各种各样的注意安全... 冰茶姬: 原来再打食物链时看了一下冰茶姬,只注意了路径压缩,没想到还有什么按秩排序但确实快了不少... int find( ...
随机推荐
- JAVASCRIPT闭包以及原型链
方法内部还有个方法,实例化父方法后,再次调用父方法,可以运行父方法内部的子方法,这样的程序就叫做闭包 DEMO如下: //function outerFn() { // var outerVar = ...
- Makefile入门教程
Makefile介绍 make是一个命令工具,它解释Makefile 中的指令(应该说是规则).在Makefile文件中描述了整个工程所有文件的编译顺序.编译规则.Makefile 有自己的书写格式. ...
- urllib基础-请求对象request
简单的案例-爬取百度首页 from urllib import request ''' 爬取百度首页 ''' # 确定爬去目标 base_url = 'http://www.baidu.com' # ...
- C-基础:详解sizeof和strlen,以及strstr
sizeof和strlen (string.h) 先看几个例子(sizeof和strlen之间的区别): (1) 对于一个指针, char* ss ="0123456789"; ...
- java程序-类的高级特性
创建Employee类,在类中定义三个属性:编号,姓名,年龄,然后在构造方法里初始化这三个属性,最后在实现接口中的定义的CompareTo方法,将对象按编号升序排列. 代码如下:(程序可能有些错误,方 ...
- iOS 面试集锦
是第一篇: 1.Difference between shallow copy and deep copy? 浅复制和深复制的区别? 答案:浅层复制:只复制指向对象的指针,而不复制引用对象本身. 深层 ...
- [LUOGU]1141 01迷宫
题目描述 有一个仅由数字0与1组成的n×n格迷宫.若你位于一格0上,那么你可以移动到相邻4格中的某一格1上,同样若你位于一格1上,那么你可以移动到相邻4格中的某一格0上. 你的任务是:对于给定的迷宫, ...
- NGINX宏观手记
一.这里的优化主要是指对nginx的配置优化,一般来说nginx配置文件中对优化比较有作用的主要有以下几项: nginx进程数,建议按照cpu数目来指定,一般跟cpu核数相同或为它的倍数. ``` w ...
- jsDate()
var myTime=new Date();//myTime的数据类型为(typeof) object //下面得到的都为number 类型 getFullYear();年 四位数字返回年份. get ...
- windows10系统下安装keras框架以theano为后端并配置gpu加速
在安装之前,请确保你的显卡是NVIDIA的,并且是以下型号,否则不能进行gpu加速,右键我的电脑--管理--设备管理器--显示适配器.另外如果你的电脑是windows7,安装教程也是一样的,不过根据k ...