Phone List
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 26385   Accepted: 7957

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
静态建树,动态建树TLE.
#include"cstdio"
#include"cstdlib"
#include"cstring"
using namespace std;
const int MAXN=;
const int N=;
bool flag;
struct node{
bool val;
node* next[N];
node()
{
val=false;
for(int i=;i<N;i++) next[i]=NULL;
}
};
node memory[MAXN];
int ant;
node *root;
void insert(char *s)
{
//?Xn?Yn???
node* p=root;
for(int i=;s[i];i++)
{
int k=s[i]-'';
if(p->next[k]==NULL) p->next[k]=&memory[ant++];//now node();
p=p->next[k];
if(p->val==true)//Xn?Yn????
{
flag=true;
}
}
p->val=true;
for(int i=;i<N;i++)
{
if(p->next[i]!=NULL)//?Yn?Xn??????????next[i]??NULL
{
flag=true;
break;
}
} }
/*
void del(node *p)
{
for(int i=0;i<N;i++)
{
if(p->next[i]!=NULL)
{
del(p->next[i]);
}
}
delete p;
}
*/
int main()
{
int T;
scanf("%d",&T);
for(int t=;t<=T;t++)
{
ant=;memset(memory,,sizeof(memory));
flag=false;
root=&memory[ant++];//new node();
int n;scanf("%d",&n);
char phone[];
while(n--)
{
scanf("%s",phone);
if(!flag) insert(phone);
}
if(flag) printf("NO\n");
else printf("YES\n");
//del(root);
} return ;
}

Java版:

import java.util.Scanner;
class Node {
boolean val;
Node[] net = new Node[10];
}
public class Main {
Scanner in = new Scanner(System.in);
int n;
Node root;
boolean insert(String str) {
Node p = root;
for(int i = 0, size = str.length(); i < size; ++i) {
int k = str.charAt(i) - '0';
if(p.net[k] == null) {
p.net[k] = new Node();
} else {
Node q = p.net[k];
int time = 0;
for(int j = 0; j < 10; ++j) {
if(q.net[j] == null) {
time++;
} else break;
}
if(time == 10) {
return true;
}
}
p = p.net[k];
p.val = true;
}
for(int i = 0; i < 10; i++) {
if(p.net[i] != null) {
return true;
}
}
return false;
}
public Main() {
int T = in.nextInt();
String str;
boolean tag;
while(T-- != 0) {
root = new Node();
n = in.nextInt();
tag = false;
while(n-- != 0) {
str = in.next();
if(tag) continue;
if(insert(str)) {
tag = true;
}
}
if(tag) System.out.println("NO");
else System.out.println("YES");
}
}
public static void main(String[] args) {
new Main();
}
}

POJ3630(Trie树)的更多相关文章

  1. HihoCoder第二周与POJ3630:Trie树的建立

    这又是两道一样的题,都是建立trie树的过程. HihoCoder第二周: 这里其实逻辑都很简单,主要在于数据结构struct的使用. #include <iostream> #inclu ...

  2. POJ3630——简单Trie树

    这个题的意思是说,给出一些字符串,判断是否有字符串是另一个字符串的前缀,当然可以用排序水过,不过这个题拿来练习一下Trie树不错. 这个题在poj的discuss上好多人说必须要静态建树,估计都是用了 ...

  3. poj3630 Phone List【Trie树】

    Phone List Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 34805   Accepted: 9980 Descr ...

  4. poj3630 Phone List (trie树模板题)

    Phone List Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26328   Accepted: 7938 Descr ...

  5. 蒟蒻的trie树专题

    POJ 3630 Phone List: 模板 ///meek #include<bits/stdc++.h> using namespace std; using namespace s ...

  6. 基于trie树做一个ac自动机

    基于trie树做一个ac自动机 #!/usr/bin/python # -*- coding: utf-8 -*- class Node: def __init__(self): self.value ...

  7. 基于trie树的具有联想功能的文本编辑器

    之前的软件设计与开发实践课程中,自己构思的大作业题目.做的具有核心功能,但是还欠缺边边角角的小功能和持久化数据结构,先放出来,有机会一点点改.github:https://github.com/chu ...

  8. hihocoder-1014 Trie树

    hihocoder 1014 : Trie树 link: https://hihocoder.com/problemset/problem/1014 题意: 实现Trie树,实现对单词的快速统计. # ...

  9. 洛谷P2412 查单词 [trie树 RMQ]

    题目背景 滚粗了的HansBug在收拾旧英语书,然而他发现了什么奇妙的东西. 题目描述 udp2.T3如果遇到相同的字符串,输出后面的 蒟蒻HansBug在一本英语书里面找到了一个单词表,包含N个单词 ...

随机推荐

  1. HTML元素嵌套关系

  2. litou.org.cn: A Shunde Program Fan.

    终于有了自己的域名litou.org.cn.(虽然并不是什么难的事) 作为顺德的一名编程爱好者,编程就是全部,希望可以多分享一些设计和开发经验. 一份耕耘,一份收获.My Name is 泥头.

  3. Redis的主从同步手动执行故障切换

    1.准备三个redis配置文件,通过端口的区分,启动三个redis数据库实例,然后配置主从复制. # a6371.conf port 6371 daemonize yes pidfile /data/ ...

  4. 如何更改CSDN博客高亮代码皮肤的样式,使博客看起来更有范(推荐)

    由于本人写博客的时候,也没有配置博客的相关属性,因此贴出来的代码块都是CSDN默认的,因此代码背景色都是白色的,如下所示: 但是本人在浏览他人博客的时候,发现有些博客的代码块看起来比较有范,整个代码库 ...

  5. JSP 分页代码

    jsp 分页模板 后台分页代码: 说明: 在 com.zc.domain 包下: PageBean.java 文件 package cn.itcast.customer.domain;   impor ...

  6. Jquery的parent和parents(找到某一特定的祖先元素)用法(转发:https://blog.csdn.net/cui_angel/article/details/7903704)

    <!-- parent是指取得一个包含着所有匹配元素的唯一父元素的元素集合. parents则是取得一个包含着所有匹配元素的祖先元素的元素集合(不包含根元素).可以通过一个可选的表达式进行筛选. ...

  7. MYSQL:基础——触发器

    MYSQL基础——触发器 引入触发器 什么是触发器 如果你想要某条语句(或某些语句)在事件发生时自动执行.比如: 每当订购一个产品时,都从库存数量中减去订购的数量:无论何时删除一行,都在某个存档表中保 ...

  8. yii 资料

    https://github.com/forecho/awesome-yii2 会随时更新 链接:http://pan.baidu.com/s/1mgCKtUK 密码:t6t1 与<YII框架& ...

  9. 【leetcode刷题笔记】Spiral Matrix II

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  10. XShell 连接虚拟机中的服务器 失败 、连接中断(Connection closed by foreign host.)

    在使用XShell连接虚拟机中的服务器时,报以下错误并断开连接,之前连接还是挺稳定的,忽然就这样了 Last login: Thu Aug :: from 192.168.1.102 [root@no ...