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个单词 ...
随机推荐
- php读写csv、xml文件: SimpleExcel
实例结构: 1. csv2xml.demo.php <?php use SimpleExcel\SimpleExcel; // 这句不能少! require_once ('../lib/Simp ...
- CSS图片居中,多余隐藏
/*外层DIV*/ div {position: relative;overflow:hidden;width: 显示宽度px;} /*left=50%刚好在中间,margin-left=往前移动图片 ...
- ASP获取上月本月下月的第一天和最后一天
上月第一天:<%=dateadd("m",-1,year(date)&"-"&month(date)&"-1" ...
- c++操作flash
c++操作falsh,忘了原文在哪了,自己尝试了,直接贴代码 // SDK版本 //////////////////////////////////////////////////////////// ...
- kettle连接资源库设置
到这里你是登陆不上去的,需要创建或更新按钮,因为需要在你的数据库里创建关于kettle的数据表,来存储资源库 点执行就可以了 一般情况下kettle资源库自动给你创建两个用户: 工具->资源库- ...
- Javaweb基础--->过滤器filter(转发)
一.Filter简介 Filter也称之为过滤器,它是Servlet技术中最激动人心的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, 静态 ...
- QT设置textEdit光标到末尾
//移动光标到末尾 QTextCursor cursor = ui->receiveTextEdit->textCursor(); cursor.movePosition(QTextCur ...
- 3.11课·········C#类
String类:.Length 字符的长度,返回一个int类型的值 .Trim() 去掉开头以及结尾的空格.TrimStart() 去掉字符串开头的空格.TrimEnd() 去掉字符串后面的空格 .T ...
- 每天一个Linux命令(31)diff命令
diff命令在最简单的情况下,比较给定的两个文件的不同.如果使用“-”代替“文件”参数,则要比较的内容将来自标准输入.diff命令是以逐行的方式,比较文本文件的异同处.如果该命令指定进行目录的比较,则 ...
- 【leetcode刷题笔记】Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...