传送门

Phone List

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11633    Accepted Submission(s): 3965

Problem 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:
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.
 
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
98346
 
Sample Output
NO
YES
 
Source
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  1075 1251 1247 1677 1298 
 
前面一个是字典树
 
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]的更多相关文章

  1. Colored Sticks POJ - 2513 并查集+欧拉通路+字典树hash

    题意:给出很多很多很多很多个棒子 左右各有颜色(给出的是单词) 相同颜色的可以接在一起,问是否存在一种 方法可以使得所以棒子连在一起 思路:就是一个判欧拉通路的题目,欧拉通路存在:没奇度顶点   或者 ...

  2. HDU1671 水题字典树

    #include<cstdio> #include<cstdlib> #include<iostream> #include<cstring> #inc ...

  3. hihoCoder 1014trie树(字典树)

    hihoCoder 1014 题目提示已经很清楚了~ 贴代码…… #include <iostream> #include <cstdio> #include <cstr ...

  4. POJ2513Colored Sticks(欧拉通路)(字典树)(并查集)

                                                             Colored Sticks Time Limit: 5000MS   Memory ...

  5. poj 2503 Babelfish(Map、Hash、字典树)

    题目链接:http://poj.org/bbs?problem_id=2503 思路分析: 题目数据数据量为10^5, 为查找问题,使用Hash或Map等查找树可以解决,也可以使用字典树查找. 代码( ...

  6. 字符串hash与字典树

    title: 字符串hash与字典树 date: 2018-08-01 22:05:29 tags: acm 算法 字符串 概述 这篇主要是关于字符串里的 字符串hash 和 字符串字典树,,两个都是 ...

  7. hash(2018年CSUST省赛选拔赛第一场B题+hash+字典树)

    题目链接:http://csustacm.com:4803/problem/1006 题目: 思路:正如题目一样,本题是一个hash,比赛的时候用的字典树,但是不知道为什么一直RE(听学长说要动态开点 ...

  8. POJ 2503 Babelfish(map,字典树,快排+二分,hash)

    题意:先构造一个词典,然后输入外文单词,输出相应的英语单词. 这道题有4种方法可以做: 1.map 2.字典树 3.快排+二分 4.hash表 参考博客:[解题报告]POJ_2503 字典树,MAP ...

  9. day 1 堆 hash 线段树 树状数组 冰茶姬 字典树 二叉查找树

    来郑州的第二天,早上开始也没说什么就说了些注意安全,各种各样的注意安全... 冰茶姬: 原来再打食物链时看了一下冰茶姬,只注意了路径压缩,没想到还有什么按秩排序但确实快了不少... int find( ...

随机推荐

  1. Gym 100883J palprime(二分判断点在凸包里)

    题意:判断一堆小点有多少个在任意三个大点构成的三角形里面. 思路:其实就是判断点在不在凸包里面,判断的话可以使用二分来判断,就是判断该点在凸包的哪两个点和起点的连线之间. 代码: /** @xigua ...

  2. Java poi 的使用

    poi可操作老旧版本的excel 下载jar包,http://archive.apache.org/dist/poi/release/bin/poi-bin-3.17-20170915.tar.gz ...

  3. 新版raspbian系统的固定IP配置和启用root账户的ssh登录功能的方法

    1. 2016新版raspbian系统的固定IP配置: 自2016年2月份新版raspbian系统发布以后,树莓派的固定IP配置方法就与之前不一样了. 之前在raspbian系统中编辑/etc/net ...

  4. Vue-Quill-Editor 富文本编辑器的使用

    步骤如下: 1.下载Vue-Quill-Editor npm install vue-quill-editor --save 2.下载quill(Vue-Quill-Editor需要依赖) npm i ...

  5. MySQL查询当天数据以及大量查询时提升速度

    select * from 表名 where to_days(字段名) = to_days(now()) 一.数据库设计方面1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 ord ...

  6. ubuntu frp 自编译。本文不能按顺序来 请自己理解

    go run:go run 编译并直接运行程序,它会产生一个临时文件(但不会生成 .exe 文件),直接在命令行输出程序执行结果,方便用户调试. go build:go build 用于测试编译包,主 ...

  7. [LUOGU] P2679 子串

    一开始用一个f数组转移,发现不太对,状态有重叠部分 f[i][j][k]表示考虑了s的前i位,匹配到t的第j位,用了k个子串,且s的第i位必选 g[i][j][k]表示考虑了s的前i位,匹配到t的第j ...

  8. docker参考文档

    docker 使用笔记 http://www.cnblogs.com/xguo/p/3829329.html docker数据存储 | 单线程 http://opjasee.com/2014/06/2 ...

  9. Python字符串操作详解

    菜鸟学Python第五天 流程控制 for循环 while循环 VS for循环: while循环:称之为条件循环,循环的次数取决于条件何时为false for循环:称之为迭代器循环,循环的次数取决于 ...

  10. (转))iOS App上架AppStore 会遇到的坑

    iOS App上架AppStore 会遇到的坑   前言:非原创 文章摘自:http://zhuanlan.zhihu.com/100000PM/20010725 相信大家一定非常「深恶痛疾」AppS ...