hdu Phone List
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
算法:字典树
题意:给你一些单词让你判断这单词中,如果有单词作为另一个的前缀就输出NO;否则YES
代码:
- #include <stdio.h>
- #include <algorithm>
- #include <string.h>
- #include <iostream>
- using namespace std;
- #define Max 20
- struct dot
- {
- dot *next[Max];
- int flag;
- };
- dot *newnode()
- {
- dot *temp=new dot;
- int i;
- temp->flag=0;
- for(i=0;i<Max;i++)
- temp->next[i]=NULL;
- return temp;
- }
- void tree(char *st,dot *root,int &k)
- {
- int len=strlen(st);
- dot *p=root;
- int id=0;
- for(int i=0;i<len;i++)
- {
- id=st[i]-'0';
- if(p->next[id]==NULL)
- p->next[id]=newnode();
- if(p->flag)//例如之前出现单词abc,现在是abcd在这判断可以判断出
- {
- k=1;
- return ;
- }
- p=p->next[id];
- }
- p->flag=1;
- if(p->next[id]!=NULL)//例如之前出现abcd,现在是abc这在这判断出来
- {
- k=1;return ;
- }
- p->next[id]=newnode();
- }
- void del(dot *p)
- {
- if(p==0) return ;
- for(int i=0;i<Max;i++)
- if(p->next[i])
- del(p->next[i]);
- delete p;
- }
- int main()
- {
- int n,m,k;
- char st[15];
- scanf("%d",&n);
- while(n--)
- { dot *root=new dot;
- root=newnode();
- scanf("%d",&m);
- k=0;
- while(m--)
- {
- scanf("%s",&st);
- if(k==0)//如果还没出现单词为另一个单词的前缀这种情况,就继续建树
- tree(st,root,k);
- }
- if(k) printf("NO\n");
- else printf("YES\n");
- del(root);
- }
- return 0;
- }
hdu Phone List的更多相关文章
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
- HDU 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
- hdu 4481 Time travel(高斯求期望)(转)
(转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...
- HDU 3791二叉搜索树解题(解题报告)
1.题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3791 2.参考解题 http://blog.csdn.net/u013447865/articl ...
- hdu 4329
problem:http://acm.hdu.edu.cn/showproblem.php?pid=4329 题意:模拟 a. p(r)= R'/i rel(r)=(1||0) R ...
- HDU 2586
http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:求最近祖先节点的权值和 思路:LCA Tarjan算法 #include <stdio.h&g ...
随机推荐
- 在sublime text 3中安装中文支持
1.安装package control 使用control+~打开终端,然后输入以下内容并确定: import urllib.request,os;pf='Package Control.subli ...
- 解决APP中fragment重叠问题
由于内存重启,导致的frgament重叠,其原因就是FragmentState没有保存Fragment的显示状态,即mHidden,导致页面重启后,该值为默认的false,即show状态,所以导致了F ...
- POJ2406 KMP算法
POJ2406 问题重述:给定字符串s0,记连续的k个s前后相连组成的s0s0...s0为s0^k.输入字符串S,求最大的k,使得S = s0^k. 问题分析: 1.采用kmp算法求出前缀函数 pre ...
- ECstore报表不显示解决
最近研究ECSTORE发现后台报表显示空白,Google了一下发现N多统一的做法,直接往表里插几条数据.呵呵,更深入一点 1.要显示报表功能首先要确保已经配置好contab的定时任务,定时任务能够执行 ...
- flask开发restful api系列(2)
继续上一章所讲,上一章我们最后面说道,虽然这个是很小的程序,但还有好几个要优化的地方.先复制一下老的view.py代码. # coding:utf-8 from flask import Flask, ...
- C 产生随机码
#include<stdio.h>#include<malloc.h>#include<conio.h>#include<stdlib.h>#inclu ...
- Android 颜色大全 (colors.xml )
<resources> <color name="pink">#ffc0cb</color><!--粉红色 --> <colo ...
- 这样就算会了PHP么?-9
PHP关于COOKIE的应用 <?php if (!isset($_COOKIE["visittime"])) { setcookie("visittime&quo ...
- mysql常用查询归纳
一.mysql查询的五种子句 where(条件查询).having(筛选).group by(分组).order by(排序).limit(限制结果数) .where常用运算符: 比较运算符 > ...
- 解决Jenkins上git出现的“ERROR: Error fetching remote repo 'origin'”问题
今天对清掉了Jenkins中项目的工作空间,结果构建出现“ERROR: Error fetching remote repo 'origin'”问题:网上各种找也没找到解决这个问题的方法. 后来看错误 ...