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. Cloneable 和clone的区别和联系

    设计模式----原型模式时候,涉及到的复制克隆, Cloneable 接口,内部是没有任何方法的, 这个接口其实是一个标记性的接口,和Serializable是一样的,都是标记使用, 在类实现了这个C ...

  2. 京东android面试题(2018 顶级互联网公司面试题系列)

    以下来自于北京的一个兄弟的面试题 1.静态内部类和非静态内部类有什么区别  2.谈谈你对java多态的理解  3.如何开启线程,run和runnable有什么区别  4.线程池的好处  5.说一下你知 ...

  3. 启动/关闭Spring boot服务脚本

    启动Spring boot服务脚本 #!/bin/bash cd /test java -jar test.jar &> ./test.log & echo "成功&q ...

  4. 阿里云服务(一) OSS

    阿里电子商务迄今是中国最大的电商网站,各个厂商都在去模仿.就像google的大数据处理,Hadoop的思想等等,只有做出了一些成绩,起了带头羊,那么将会是非常吃香的.从今天开始简单学习了解一下阿里的各 ...

  5. SMARTFORMS自定义打印格式

    [转自 http://lz357502668.blog.163.com/blog/static/16496743201272155135570/] 在sap的打印开发中经常需要自定义纸张,具体步骤如下 ...

  6. ADO.NET概述

    xml这类文件它是.net变成环境中优先使用的数据访问借口. ADO.NET传输的数据都是XML格式的 ADO.NET是一组用于和数据源惊醒交互的面向对象类库 数据源:通常是各种数据库,但文本.exc ...

  7. Kattis - entertainmentbox 【贪心】

    思路 先将 N 个 电视节目 排序 根据 结束时间 ,结束的早的 排在前面 然后 弄 K个标记 记录 结束时间 然后 遍历一下 每次 如果能插入的话 插入到 结束时间最小的那个 队列里面去然后 每次插 ...

  8. [原创]java WEB学习笔记29:Cookie Demo 之自动登录

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  9. 319 Python基础之格式化输出、逻辑运算符、编码、in not in、while else、

    一.格式化输出 占位符%,字符串占位符%s,数字占位符%d 第一种name = input('姓名') age = input('年龄') hobby = input ("爱好") ...

  10. P2455 [SDOI2006]线性方程组

    P2455 [SDOI2006]线性方程组 真\(\cdot\)高斯消元模板题 由于各种hack数据被造出来~码量突增~,其实也就多了二三十行 将每行系数消到最多有一个非0数 特殊情况: 在过程同时 ...