POJ3630(Trie树)
| 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树)的更多相关文章
- HihoCoder第二周与POJ3630:Trie树的建立
这又是两道一样的题,都是建立trie树的过程. HihoCoder第二周: 这里其实逻辑都很简单,主要在于数据结构struct的使用. #include <iostream> #inclu ...
- POJ3630——简单Trie树
这个题的意思是说,给出一些字符串,判断是否有字符串是另一个字符串的前缀,当然可以用排序水过,不过这个题拿来练习一下Trie树不错. 这个题在poj的discuss上好多人说必须要静态建树,估计都是用了 ...
- poj3630 Phone List【Trie树】
Phone List Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 34805 Accepted: 9980 Descr ...
- poj3630 Phone List (trie树模板题)
Phone List Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26328 Accepted: 7938 Descr ...
- 蒟蒻的trie树专题
POJ 3630 Phone List: 模板 ///meek #include<bits/stdc++.h> using namespace std; using namespace s ...
- 基于trie树做一个ac自动机
基于trie树做一个ac自动机 #!/usr/bin/python # -*- coding: utf-8 -*- class Node: def __init__(self): self.value ...
- 基于trie树的具有联想功能的文本编辑器
之前的软件设计与开发实践课程中,自己构思的大作业题目.做的具有核心功能,但是还欠缺边边角角的小功能和持久化数据结构,先放出来,有机会一点点改.github:https://github.com/chu ...
- hihocoder-1014 Trie树
hihocoder 1014 : Trie树 link: https://hihocoder.com/problemset/problem/1014 题意: 实现Trie树,实现对单词的快速统计. # ...
- 洛谷P2412 查单词 [trie树 RMQ]
题目背景 滚粗了的HansBug在收拾旧英语书,然而他发现了什么奇妙的东西. 题目描述 udp2.T3如果遇到相同的字符串,输出后面的 蒟蒻HansBug在一本英语书里面找到了一个单词表,包含N个单词 ...
随机推荐
- MySQL合并多行
select id,group_concat(re_id order by re_id separator ",") as re_idfrom tablenamegroup by ...
- 我的Android进阶之旅------>android中getLocationInWindow 和 getLocationOnScreen的区别
View.getLocationInWindow(int[] location) 一个控件在其父窗口中的坐标位置 View.getLocationOnScreen(int[] location) 一个 ...
- scala如何解决类型强转问题
scala如何解决类型强转问题 scala属于强类型语言,在指定变量类型时必须确定数据类型,即便scala拥有引以为傲的隐式推到,这某些场合也有些有心无力. 例如: java同属强类型语言,但java ...
- MyBaits面试题
原文地址 Mybatis技术内幕系列博客,从原理和源码角度,介绍了其内部实现细节,无论是写的好与不好,我确实是用心写了,由于并不是介绍如何使用Mybatis的文章,所以,一些参数使用细节略掉了,我们的 ...
- 【TFS】解决TFS编译中文乱码问题
前言; TFS2018做程序集成非常方便,线上编译然后直接生成docker镜像,但是在使用过程中遇到编译窗口中文乱码的问题,这个问题找了好久没人知道怎么解决.如下: 这个问题不解决,每次编译失败,研发 ...
- 中国移动OnetNet云平台 使用WIFI模块ESP8266 TCP非透传模式传输数据流步骤
测试使用工具: WIFI模块型号:ESP8266 https://item.taobao.com/item.htm?spm=a1z10.1-c.w137712-175513579.2.btbD9X&a ...
- HDU - 5695 Gym Class 【拓扑排序】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5695 思路 给定一些关系 进行拓扑排序 但是有一个要求 对于哪些没有确切的位置的点 要按照ID大小 I ...
- iOS base64编码 MD5 加密
//创建一个Base64编码的NSString对象 //字符串 转二进制 NSData *nsdata = [@"iOS Developer Tips encoded in Base64&q ...
- 关于tomcate跨域配置的配置问题和表头加入新属性的过滤
1 .在项目中常常遇到本地访问服务器上的链接数据访问不到,并出现如下问题: 这是因为tomcate 的配置中过滤了请求方式, 解决方案: 1.在tomcate中引入两个jar包:java-proper ...
- Android平台及其架构(部分转载)
一.Android的系统架构 1. 应用程序 同Android系统一起发布的核心应用程序,如email 客户端,SMS 短消息程序,日历,地图,浏览器,联系人管理程序等. 这些应用程序都是用 ...