码迷,mamicode.com
首页 > 其他好文 > 详细

Phone List

时间:2018-02-25 17:24:51      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:情况   rmi   std   def   题意   动态插入   fine   number   blog   

Time Limit: 1000MS   Memory Limit: 65536K

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
Source

Nordic 2007

题意:多组数据,对于每组数据

有n个数字串,问这n个串中是否有一个串是另一个串的前缀

如果有,输出“NO”否则输出“YES”

题解:

用Tire树实现,但是因为多组数据,每一次都要清空

所以在插入的时候在清空,并且动态插入

否则要么RE,要么MLE

特殊情况详见代码

 

技术分享图片
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cmath>
 5 #define ll long long
 6 #include<cstring>
 7 #include<string>
 8 #define DB double
 9 using namespace std;
10 int T,n,tot,t[100010][10];
11 bool fg,v[100100],mark;
12 char s[100010];
13 void ins(int len)
14 {
15     int now=0;mark=0;
16     for(int i=1;i<=len;++i)
17     {
18         int k=s[i]-0;
19         if(!t[now][k]) t[now][k]=++tot,mark=1;
20         //这是一个新的节点,说明到现在为止,这个串新开辟了位置,肯定不是之前某个串的前缀
21         now=t[now][k];
22         if(v[now]) fg=1;
23         //遇到了之前某个串的结尾。。肯定不符合题意
24     }
25     if(!mark) fg=1;
26     //到插入整个字符了还没有开辟新位置,显然新插入的字符串是之前某个串的前缀
27     v[now]=1;
28 }
29 int main()
30 {
31     scanf("%d",&T);
32     while(T--)
33     {
34         tot=0;fg=0;
35         memset(t,0,sizeof(t));
36         memset(v,0,sizeof(v));
37         scanf("%d",&n);
38         for(int i=1;i<=n;++i)
39         {
40             scanf("%s",s+1);
41             if(!fg)ins(strlen(s+1));
42         }
43         if(fg) puts("NO");
44         else puts("YES");
45     }
46     return 0;
47 }
View Code

 

若言言悦耳,事事悦心,便把此生埋在鸩毒中矣。

 

Phone List

标签:情况   rmi   std   def   题意   动态插入   fine   number   blog   

原文地址:https://www.cnblogs.com/adelalove/p/8469543.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!