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( ...
随机推荐
- flask模板语言
由于Django的模板引擎和Flask中的Jinja2模板引擎有很多一样的地方,所以我将一样的地方总结到了独立的文章中 https://www.cnblogs.com/kuxingseng95/art ...
- excel数据导入到数据库
package test; import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundExceptio ...
- iOS旋屏
横竖屏切换,视图乱了怎么办? 首先,我们必须了解一下下列4种状态,它们被用来描述设备旋转方向: UIInterfaceOrientationLandscapeLeft 向左,即HOME键在右 UIIn ...
- 全排列问题(DFS)
题目描述: 输出自然数1到n所有不重复的排列,即n的全排列,要求所产生的任一数字序列中不允许出现重复的数字. 输入格式: n(1≤n≤9) 输出格式: 由1-n组成的所有不重复的数字序列,每行一个序列 ...
- python 连接redis cluster
#!/usr/bin/env python # encoding: utf-8 #@author: 东哥加油! #@file: clear_pool.py #@time: 2018/8/28 17:0 ...
- Python操作12306抢票脚本
有一段时间没有使用Python了,前几天经朋友提起一篇关于用Python实现抢火车票的文章,百度了实现抢火车票的技术细节,网上却有不少资料,也不是新鲜的东西.在了解了一些技术手段,阅读了一些大神的博文 ...
- 创建yum仓库
第1章 服务端)创建yum仓库目录 命令:(创建)mkdir -p /application/yum/centos6.6/x86_64/ 命令:(切换)cd /application/yum/cent ...
- shell脚本中使用echo显示带颜色的内容
shell脚本中使用echo显示带颜色的内容,需要使用参数-e 格式如下: echo -e "\033[字背景颜色;文字颜色m字符串\033[0m" 例如: echo -e &qu ...
- Vue 2.0 项目在IE下显示空白
新写的项目在 IE浏览器显示空白 解释一: Babel默认只转换新的JavaScript句法(syntax),而不转换新的API,比如Iterator.Generator.Set.Maps.Proxy ...
- InnoDB体系架构总结(二)
事务 确保事务内的SQL都可以同步执行 要么一起成功 要么一起失败.事务有四个特性原子性 一致性,隔离性,持久性 实现方式 开始事务的时候回家记录记录一个LSN日志序列 当事务执行的时候 会首先在In ...