poj 3630 Phone List trie树
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:
- Emergency 911
- Alice 97 625 999
- 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
- #include<iostream>
- #include<cstdio>
- #include<cmath>
- #include<string>
- #include<queue>
- #include<algorithm>
- #include<stack>
- #include<cstring>
- #include<vector>
- #include<list>
- #include<set>
- #include<map>
- using namespace std;
- #define ll long long
- #define mod 1000000007
- #define pi (4*atan(1.0))
- const int N=1e5+,M=5e6+,inf=1e9+;
- int a[N][],sum[M],len;
- void init()
- {
- memset(a,,sizeof(a));
- memset(sum,,sizeof(sum));
- len=;
- }
- int getnum(char a)
- {
- return a-'';
- }
- void insertt(char *aa)
- {
- int u=,n=strlen(aa);
- for(int i=; i<n; i++)
- {
- int num=getnum(aa[i]);
- if(!a[u][num])
- {
- a[u][num]=len++;
- }
- u=a[u][num];
- sum[u]++;
- }
- }
- int getans(char *aa)
- {
- int u=,x=strlen(aa);
- for(int i=; i<x; i++)
- {
- int num=getnum(aa[i]);
- if(!a[u][num])
- return ;
- u=a[u][num];
- }
- return sum[u];
- }
- char ch[N][];
- int main()
- {
- int x,y,z,i,t;
- int T;
- scanf("%d",&T);
- while(T--)
- {
- scanf("%d",&x);
- init();
- for(i=; i<x; i++)
- {
- scanf("%s",ch[i]);
- insertt(ch[i]);
- }
- int flag=;
- for(i=;i<x;i++)
- {
- if(getans(ch[i])!=)
- {
- flag=;
- break;
- }
- }
- if(flag)
- printf("YES\n");
- else
- printf("NO\n");
- }
- return ;
- }
poj 3630 Phone List trie树的更多相关文章
- POJ 3630 Phone List | Trie 树
题目: 给定 n 个长度不超过 10 的数字串,问其中是否存在两个数字串 S, T ,使得 S 是 T 的前缀.多组数据,数据组数不超过 40. 题解: 前缀问题一般都用Trie树解决: 所以跑一个T ...
- poj 2513 Colored Sticks (trie 树)
链接:poj 2513 题意:给定一些木棒.木棒两端都涂上颜色,不同木棒相接的一边必须是 同样的颜色.求能否将木棒首尾相接.连成一条直线. 分析:能够用欧拉路的思想来解,将木棒的每一端都看成一个结点 ...
- [ACM] POJ 2418 Hardwood Species (Trie树或map)
Hardwood Species Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 17986 Accepted: 713 ...
- [POJ 1204]Word Puzzles(Trie树暴搜&AC自己主动机)
Description Word puzzles are usually simple and very entertaining for all ages. They are so entertai ...
- poj 3630 Phone List(字典树)
题目链接: http://poj.org/problem?id=3630 思路分析: 求在字符串中是否存在某个字符串为另一字符串的前缀: 即对于某个字符串而言,其是否为某个字符串的前缀,或存在某个其先 ...
- poj 2513 Colored Sticks trie树+欧拉图+并查集
点击打开链接 Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27955 Accepted ...
- POJ 3630 Phone List Trie题解
Trie的应用题目. 本题有两个难点了: 1 动态建立Trie会超时,须要静态建立数组,然后构造树 2 推断的时候注意两种情况: 1) Tire树有133,然后插入13333556的时候.2)插入顺序 ...
- POJ训练计划2418_Hardwood Species(Trie树)
解题报告 Tire树. #include <iostream> #include <cstring> #include <cstdio> #include < ...
- poj 2513 Colored Sticks (trie树+并查集+欧拉路)
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 40043 Accepted: 10406 ...
随机推荐
- 【BZOJ4144】[AMPPZ2014]Petrol 最短路+离线+最小生成树
[BZOJ4144][AMPPZ2014]Petrol Description 给定一个n个点.m条边的带权无向图,其中有s个点是加油站. 每辆车都有一个油量上限b,即每次行走距离不能超过b,但在加油 ...
- 使用SSH工具连接到MySQL
在SSH中查看MySQL数据信息 格式为:mysql -h主机地址 -u用户名 -p用户密码 -P端口号 -D数据库名称 例如: mysql -uroot -p123456 -h192.168.1.1 ...
- maven学习(二)(转)
一.maven父工程与子模块的拆分与聚合原理 问题描述:将ssh工程拆分为多个模块开发 1.1.拆分原理 创建一个maven project(pom),然后在创建三个子模块(maven moudule ...
- apache+tomcat实现session共享
apache+tomcat上篇文章,实现了负载均衡,现在我们实现session共享 一.tomcat集群配置,session 同步配置: tomcat1配置 A.修改Engine节点信息: < ...
- asp.net mvc(一) ----------简单封装成通用的List<T>集合
asp.net mvc(一) 这些天开始学习asp.net mvc,用传统的asp.net已经快四的年了,刚开始接触asp.net mvc确认感觉有点不适应,主要体现在asp.net mvc的实现上. ...
- X-UA-Compatible也无法解决的IE11兼容问题
3月8日接到一位用户的电话,说写博客时编辑器显示不出来.浏览器用的是披着360外衣的IE11,编辑器用的是CuteEditor. 当时电脑上没安装IE11,用IE10测试正常,心想应该是一个手到擒来的 ...
- Python菜鸟之路:Django Admin后台管理功能使用
前言 用过Django框架的童鞋肯定都知道,在创建完Django项目后,每个app下,都会有一个urls.py文件,里边会有如下几行: from django.contrib import admin ...
- Spark集群安装和WordCount编写
一.Spark概述 官网:http://spark.apache.org/ Apache Spark™是用于大规模数据处理的统一分析引擎. 为大数据处理而设计的快速通用的计算引擎. Spark加州大学 ...
- python 面向对象 __dict__
打印 类或对象中的所有成员 类的构造函数属性 属于对象:类中的公有属性和方法等属于类 打印信息 class schoolMember(object): '''学校成员分类''' member = 0 ...
- js cookie的读写
cookie是一小段信息,以键/值对的信息保存在计算机硬盘上的字符串, cookie存储容量大概在4kb,不同的浏览器厂家对cookie大小的限制有微微的差异:cookie主要的本质是“识别”,通过识 ...