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. 【BZOJ4553】[Tjoi2016&Heoi2016]序列 cdq分治+树状数组

    [BZOJ4553][Tjoi2016&Heoi2016]序列 Description 佳媛姐姐过生日的时候,她的小伙伴从某宝上买了一个有趣的玩具送给他.玩具上有一个数列,数列中某些项的值可能 ...

  2. spring+springMVC+Mybatis 中使用@Transcational方式管理事务的配置方法

    springMVC 中,事务通常都在service层控制,当然controller层也可以用事务,只要配置配对,但通常不建议直接在controller层配事务,controller的作用是管理参数以及 ...

  3. 【css预处理器】------sass的基本语法------【巷子】

    001.安装sass 1.删除gem源:gem sources --remove https://rubygems.org/ 2.添加国内源:gem sources -a http://gems.ru ...

  4. Linq初探

    1.什么是LINQ LINQ是语言集成查询(Language Integrated Query),这项技术是在.net 3.5就已经引入的技术,极大的方便了数据的查询,他可以支持数据库.XML.ADO ...

  5. ubuntu 安装搜狗

    先按照这个选择fcitx: https://blog.csdn.net/qq_40563761/article/details/82664851 然后重启 右上角会出现键盘图片点击选configura ...

  6. 人人网张铁安:Feed系统架构分析(转)

    原文:http://www.csdn.net/article/2010-07-26/277273 继成功举办首期TUP活动后,日前在北京丽亭华苑酒店鸿运二厅,由CSDN和<程序员> 杂志联 ...

  7. Git学习-->如何通过Shell脚本自动定时将Gitlab备份文件复制到远程服务器?

    一.背景 在我之前的博客 git学习--> Gitlab如何进行备份恢复与迁移? (地址:http://blog.csdn.net/ouyang_peng/article/details/770 ...

  8. Mysql EXPLAIN 相关疑问: Using temporary ; Using filesort

    一.什么是Using temporary ; Using filesort 1. using filesort filesort主要用于查询数据结果集的排序操作,首先MySQL会使用sort_buff ...

  9. OCR技术浅探:Python示例(5)

    文件说明: 1. image.py——图像处理函数,主要是特征提取: 2. model_training.py——训练CNN单字识别模型(需要较高性能的服务器,最好有GPU加速,否则真是慢得要死): ...

  10. MySQL新加用户和开启慢查询

    mysql>grant select on *.* to read@'%' identified by 'j';  //给予read用户只读全部库的权限 mysql>grant selec ...