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. C# Global.asax文件里实现通用防SQL注入漏洞程序(适应于post/get请求)

    可使用Global.asax中的Application_BeginRequest(object sender, EventArgs e)事件来实现表单或者URL提交数据的获取,获取后传给SQLInje ...

  2. [SQL] sql server中如何查看执行效率不高的语句

    sql server中,如果想知道有哪些语句是执行效率不高的,应该如何查看呢?下面就将为您介绍sql server中如何查看执行效率不高的语句,供您参考.在测量功能时,先以下命令清除sql serve ...

  3. mybatis-config.xml配置

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...

  4. 编程中,static的用法详解

    C++的static有两种用法:面向过程程序设计中的static和面向对象程序设计中的static.前者应用于普通变量和函数,不涉及类:后者主要说明static在类中的作用.一.面向过程设计中的sta ...

  5. MVC学习之HtmlHelper

    1.为什么要使用HtmlHelper? 1.首先HtmlHelper是一个类型,MVC中的ViewPage<TModel>中的一个属性Html属性,这个属性的类型就是HtmlHelper& ...

  6. C# WebBrowser的8个方法、13个属性和事件

    1.方法 说明 GoBack 相当于IE的“后退”按钮,使你在当前历史列表中后退一项 GoForward 相当于IE的“前进”按钮,使你在当前历史列表中前进一项  GoHome 相当于IE的“主页”按 ...

  7. 转!!CMPP 网关错误码说明

    http://www.163duanxin.com/msg/1753.htm CMPP错误码说明   与中国移动代码的对应关系. MI::zzzzSMSC返回状态报告的状态值为EXPIREDMJ:zz ...

  8. django的framework优化

    1.优化framework的性能,解决restapi调用慢的问题 ①预加载,关联查询时做缓存,序列化前简单调用setup_eager_loading ,这个需要确定sql查询调用情况(根据数据库结构确 ...

  9. 内核通信之Netlink源码分析-用户内核通信原理

    2017-07-05 本节从一个小案例入手,结合源码分析下通过netlink进行内核和用户通信的流程. 内核端 按照传统CS模式,其实内核端可以作为是服务器端,用以接收用户的请求并作出处理,但是从ne ...

  10. IDEA安装以及项目初始化

    首先安装idea: 如果15.0版本安装不上就安装16.0: 点击安装文件以后,一直点下一步就可以了. 安装完成后设置: 如果没有的话,需要点击SDKs,点击+,然后选择电脑上安装的JDK. 接下来创 ...