POJ3630 Phone List

本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。

本文作者:ljh2000 
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!

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

正解:Trie解题报告:  1A就是爽。  询问有没有一个串是另一个串的前缀。  建一棵Trie树,然后每次check一下,当前单词的最后一个结点是不是之前已经存在,存在则说明是另一个串的前缀;  如果中间经过了某个结点是之前的某个串的结束结点,也说明存在前缀。  打个标记即可。
//It is made by ljh2000
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long LL;
const int MAXN = 100011;
int n,tr[MAXN][10],cnt;
bool ok,vis[MAXN];
char ch[12];
inline int getint(){
    int w=0,q=0; char c=getchar(); while((c<‘0‘||c>‘9‘) && c!=‘-‘) c=getchar();
    if(c==‘-‘) q=1,c=getchar(); while (c>=‘0‘&&c<=‘9‘) w=w*10+c-‘0‘,c=getchar(); return q?-w:w;
}

inline void read(){
	if(!ok) { scanf("%s",ch); return ; }
	char c=getchar(); int u=0,now; bool flag;
	while(c>‘9‘ || c<‘0‘) c=getchar();
	while(1) {
		now=c-‘0‘; if(vis[u]) ok=false;
		if(!tr[u][now]) tr[u][now]=++cnt,flag=false;
		else flag=true;
		u=tr[u][now];
		c=getchar(); if(c<‘0‘||c>‘9‘) break;
	}
	if(vis[u] || flag) { ok=false; return ; }
	vis[u]=1;
}

inline void work(){
	int T=getint();
	while(T--) {
		memset(tr,0,sizeof(tr)); memset(vis,0,sizeof(vis));
		cnt=0; ok=true;
		n=getint(); for(int i=1;i<=n;i++) read();
		if(ok) puts("YES"); else puts("NO");
	}
}

int main()
{
    work();
    return 0;
}

  

时间: 2024-11-05 07:10:16

POJ3630 Phone List的相关文章

poj3630 Phone List (trie树模板题)

Phone List Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26328   Accepted: 7938 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 catalogu

poj--3630+字典树基础题

先按长度排序,先先插入长的,在插入的时候同时进行查询. <span style="font-size:18px;">#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef struct { char str[12]; }P; P p[200000]; bool cmp(P p1,

hdu杭电1671 / poj3630 字典树

传送门 题意:输入n串数字 找出是否有前缀相同的串 如果存在 输出NO否则输出YES 思路:用字典树解决 标记字典树总串的结尾 查找出一个串内部是否有被标记的节点 如果有那么说明存在前缀相同的串 在插入字典树的时候判断是否存在 AC代码: 1 #include "iostream" 2 #include "stdio.h" 3 #include "string.h" 4 using namespace std; 5 6 using namespa

Trie树 poj3630

题目链接 题目描述 有n个电话号码,长度对多为10个,问存不存在一个电话号码是另一个的前缀,是就输出NO,否则YES. 1. n<104 思路 Trie树裸题 1. 把所有字符串插入Trie树 2. 插入时进行以下判断: a. 当前插入的字符串可以沿着Tries树中的某条路径一直往下走,不用新开节点:可能比这条路径表示的字符串长,即最后才新开节点.可能比它短,即当前插入字符串读完后,Tries树的这条路径还可延伸. b. Trie树的这条路径没走完就新开节点,显然就不是前缀 代码 #includ

poj3630:Phone List——题解

http://poj.org/problem?id=3630 简单的trie树问题,先添加,然后每个跑一边看中途有没有被打上结束标记即可. #include<cstdio> #include<cstring> using namespace std; struct node{ bool ed; int son[11]; void clear(){ memset(son,0,sizeof(son)); ed=0; } }tree[100001]; char s[10001][11];

poj3630(简单tire)

#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn=100000+10; int trie[maxn][20]; char c[maxn][20]; int tot; bool endi[maxn*20]; void myinsert(char* st){ int len=strlen(st); int ch,p=1; for (int i=

POJ3630——Phone List(Trie)

(传送门) 裸Trie 1 #include<cstdio> 2 #include<iostream> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #include<cstdlib> 7 using namespace std; 8 char st[105]; 9 int ch[100005][105],sum; 10 bool bo[100005]; 1

poj3630 Phone List【Trie树】

Phone List Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 34805   Accepted: 9980 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 catalogu

POJ3630

Tire树裸题,一开始写动态的字典树,然后TLE,每次new一个新节点耗费时间较多.后来改成数组模拟的. //#include <bits/stdc++.h> #include <cstdio> #include <cstring> #include <algorithm> using namespace std ; const int maxN = 10010 ; struct stringStruct { char str[ 20 ] ; int len