Phone List

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

题意:如果有一个字符串是别的字符串的前缀就输出NO,否则YES;
思路:最基本的trie树应用;
#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树的更多相关文章

  1. POJ 3630 Phone List | Trie 树

    题目: 给定 n 个长度不超过 10 的数字串,问其中是否存在两个数字串 S, T ,使得 S 是 T 的前缀.多组数据,数据组数不超过 40. 题解: 前缀问题一般都用Trie树解决: 所以跑一个T ...

  2. poj 2513 Colored Sticks (trie 树)

    链接:poj 2513 题意:给定一些木棒.木棒两端都涂上颜色,不同木棒相接的一边必须是 同样的颜色.求能否将木棒首尾相接.连成一条直线. 分析:能够用欧拉路的思想来解,将木棒的每一端都看成一个结点 ...

  3. [ACM] POJ 2418 Hardwood Species (Trie树或map)

    Hardwood Species Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 17986   Accepted: 713 ...

  4. [POJ 1204]Word Puzzles(Trie树暴搜&amp;AC自己主动机)

    Description Word puzzles are usually simple and very entertaining for all ages. They are so entertai ...

  5. poj 3630 Phone List(字典树)

    题目链接: http://poj.org/problem?id=3630 思路分析: 求在字符串中是否存在某个字符串为另一字符串的前缀: 即对于某个字符串而言,其是否为某个字符串的前缀,或存在某个其先 ...

  6. poj 2513 Colored Sticks trie树+欧拉图+并查集

    点击打开链接 Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 27955   Accepted ...

  7. POJ 3630 Phone List Trie题解

    Trie的应用题目. 本题有两个难点了: 1 动态建立Trie会超时,须要静态建立数组,然后构造树 2 推断的时候注意两种情况: 1) Tire树有133,然后插入13333556的时候.2)插入顺序 ...

  8. POJ训练计划2418_Hardwood Species(Trie树)

    解题报告 Tire树. #include <iostream> #include <cstring> #include <cstdio> #include < ...

  9. poj 2513 Colored Sticks (trie树+并查集+欧拉路)

    Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 40043   Accepted: 10406 ...

随机推荐

  1. jenkins前端构建

    nginx 安装yum install nginx //输入下载 or yum install epel-release //如果上一步安装失败 yum install nginx //再次下载 配置 ...

  2. WebConfig配置详解大全

    <?xml version="1.0"?> <!--注意: 除了手动编辑此文件以外,您还可以使用 Web 管理工具来配置应用程序的设置.可以使用 Visual S ...

  3. 40个DBA日常维护的SQL脚本

    1.查询碎片程度高的表 条件为什么block>100,因为一些很小的表,只有几行数据实际大小很小,但是block一次性分配就是5个(11g开始默认一次性分配1M的block大小了,见create ...

  4. Python开发【模块】:M2Crypto RSA加密、解密

    M2Crypto 模块 快速安装: # 环境centos7.0,提前装好openssl(自行百度安装),windows装不上,暂不考虑了 [root@localhost ~]# pip install ...

  5. MongoDB Windows环境安装及配置( 一)

    原文http://www.cnblogs.com/lzrabbit/p/3682510.html MongoDB一般安装 1.首先到官网 (http://www.mongodb.org/downloa ...

  6. 由SOAP说开去 - - 谈谈WebServices、RMI、RPC、SOA、REST、XML、JSON

    引子: 关于SOAP其实我一直模模糊糊不太理解,这种模模糊糊的感觉表述起来是这样: 在使用web服务时(功能接口),本来我就可以通过安卓中固有的http类(使用http协议),来发送http请求,并且 ...

  7. git学习------> 解决Gitlab 版本升级之后,发送 merge request 出现 http 500 的返回码错误

    今天有同事在Gitlab上发送 Merge Request的时候,直接出现如下所示的界面,提示http 500,服务器内部出错. 一.错误描述 1.1 创建新的 Merge Request 1.2 填 ...

  8. 【Loadrunner】如何通过loadrunner利用代理对手机端Web浏览器及APP进行录制?

    以下为作者操作并最终可以成功录制脚本并调试成功的方法实际操作中记录截图,都是干货哦~ 1.安装lr11补丁 百度网盘下载链接:https://pan.baidu.com/s/1TPoaAMYvi1cw ...

  9. mysql 删除表

    删除表 DROP TABLE 表名;

  10. Windows7系统运行hadoop报Failed to locate the winutils binary in the hadoop binary path错误

    程序运行的过程中,报Failed to locate the winutils binary in the hadoop binary path  Java.io.IOException: Could ...