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. 6.2-SingletonBeanRegistry-DefaultSingletonBeanRegistry

    SingletonBeanRegistry package org.springframework.beans.factory.config; public interface SingletonBe ...

  2. [转载]设计模式的UML图

    1.抽象工厂(Abstract Factory)模式 意图:为特定的客户(或情况)提供特定系列的对象. 2.类的适配器(Adapter)模式 意图:将一个类的接口转换成客户希望的另外一个接口. 3.对 ...

  3. ssm框架与shiro的整合小demo,用idea开发+maven管理

    shiro安全框架是目前为止作为登录注册最常用的框架,因为它十分的强大简单,提供了认证.授权.加密和会话管理等功能 . shiro能做什么? 认证:验证用户的身份 授权:对用户执行访问控制:判断用户是 ...

  4. anaconda + opencv3

    直接运行 pip install opencv-python 或者 pip install opencv-contrib-python 参照如下网页 https://blog.csdn.net/sin ...

  5. php自定义的格式化时间示例代码

    时间刚好是5分钟前,则对应的时间戳就会被格式化为5分钟前,自定义的格式化时间方法如下,感兴趣的朋友可以参考下 如:时间刚好是5分钟前,则对应的时间戳就会被格式化为5分钟前,不多说了,直接贴上代码: 复 ...

  6. SpringBoot学习笔记(4):与前端交互的日期格式

    SpringBoot学习笔记(4):与前端交互的日期格式 后端模型Date字段解析String 我们从前端传回来表单的数据,当涉及时间.日期等值时,后端的模型需将其转换为对应的Date类型等. 我们可 ...

  7. Integer 与 int 中的 ==

    public class IntegerTest { public static void main(String args[]){ /** * int == 比较大小 */ int p1 = 100 ...

  8. persisted? vs new_record?

    https://joe11051105.gitbooks.io/you-need-to-know-about-ruby-on-rails/content/activerecord/persisted_ ...

  9. Docker alpine 设置东八时区

    FROM alpine:3.8 RUN echo 'http://mirrors.ustc.edu.cn/alpine/v3.5/main' > /etc/apk/repositories &a ...

  10. leetcode 1049 Last Stone Weight II(最后一块石头的重量 II)

    有一堆石头,每块石头的重量都是正整数. 每一回合,从中选出任意两块石头,然后将它们一起粉碎.假设石头的重量分别为 x 和 y,且 x <= y.那么粉碎的可能结果如下: 如果 x == y,那么 ...