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( ...
随机推荐
- 看paper的网址
http://www.arxiv-sanity.com/ https://scirate.com/ google搜cvpr open access.iccv open access
- C++ lvalue,prvalue,xvalue,glvalue和rvalue详解(from cppreference)
General 每一个C++表达式(一个操作符和它的操作数,一个字面值,一个变量名等等)都代表着两个独立属性:类型+属性分类.在现代C++中 glvalue(泛左值) = lvalue (传统意义上的 ...
- shell脚本,逻辑结构题练习。
awk '/5/{a=1}!a' file2结果:1234解释:第一行 /5/不匹配跳过{a=1},继续!a,此时a没有值属于假取反为真,故输出第一行 第二行 /5/不匹配跳过{a=1},继续!a,此 ...
- Philipp Wagner
本文大部分来自OpenCV官网上的Face Reconition with OpenCV这节内容(http://docs.opencv.org/modules/contrib/doc/facerec/ ...
- 获得Java中System对应一些属性值
public static void main(String[] args){ System.out.println("Java运行时环境版本:\n"+System.getProp ...
- TCP/IP各种数据包结构体
下面这些TCP/IP数据包是我在进行Socket及Wipcap网络编程过程中曾经用到过的数据包结构体, 这些东西平时看起来不起眼,真正用到的时候就会觉得非常有用...... 以太帧头格式结构体,共14 ...
- 【meet in middle】poj1840Eqs
震惊!map的常数居然如此之大 Description Consider equations having the following form: a1x13+ a2x23+ a3x33+ a4x43 ...
- MYSQL中批量替换某个字段的部分数据
1.修改字段里的所有含有指定字符串的文字 UPDATE 表A SET 字段B = replace(字段B, 'aaa', 'bbb') example: update table set url= ...
- verilog behavioral modeling--branch statement
conditional statement case statement 1. conditional statement if(expression) statement_o ...
- DSP中-stack和-heap的作用
-stack 0x00000800-heap 0x00000800 stack - 又称系统栈(system stack),用于: 保存函数调用后的返回地址; ...