Problem 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:
1. Emergency 911
2. Alice 97 625 999
3. 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
 
//排序比较相邻的字符串是否包含,  time:156 ms;
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <iomanip>
#include <cstdlib>
using namespace std;
const int INF=0x5fffffff;
const int MS=;
const double EXP=1e-; char str[MS][]; int cmp(const void *a,const void *b)
{
return strcmp((char *)a,(char *)b);
} int main()
{
int T;
int n,i;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(i=;i<n;i++)
scanf("%s",str[i]);
//sort(str,str+n,cmp);
qsort(str,n,sizeof(str[]),cmp);
int ok=;
for(i=;i<n&&ok;i++)
{
if(strncmp(str[i-],str[i],strlen(str[i-]))==)
ok=;
}
if(ok)
puts("YES");
else
puts("NO");
}
return ;
}
 //  静态 Trie  树法   time 78 ms
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <iomanip>
#include <cstdlib>
using namespace std;
const int INF=0x5fffffff;
const int MS=;
const double EXP=1e-; struct node
{
bool have;
node * next[];
}nodes[MS];
node *root;
bool flag;
int cnt;
node * add_node(int c)
{
node *p=&nodes[c];
for(int i=;i<;i++)
p->next[i]=NULL;
p->have=false;
return p;
}
void insert(char *str)
{
node *p=root,*q;
int len=strlen(str);
for(int i=;i<len;i++)
{
int id=str[i]-'';
if(p->next[id]==NULL)
{
q=add_node(cnt++);
p->next[id]=q;
}
// if(p->have==true)
// flag=true;
p=p->next[id];
if(p->have==true)
flag=true;
}
p->have=true;
for(int i=;i<;i++)
{
if(p->next[i]!=NULL)
{
flag=true;
break;
}
}
}
int main()
{
int i,n,t;
char str[];
scanf("%d",&t);
while(t--)
{
cnt=;
root=add_node(cnt++);
flag=false;
scanf("%d",&n);
for(i=;i<n;i++)
{
scanf("%s",str);
if(!flag)
{
insert(str);
}
}
if(flag==false)
printf("YES\n");
else
printf("NO\n");
}
return ;
}
 
 

随机推荐

  1. 【转】Hibernate利用@DynamicInsert和@DynamicUpdate生成动态SQL语句

    原文链接:http://www.cnblogs.com/quanyongan/p/3152290.html 最近在使用Hibernate4中,发现两个很有奥秘的注解 @DynamicInsert 和  ...

  2. quora 中有关angular与emberjs的精彩辩论

    原贴地址,要注册才能看,这里只有国人翻译的一部分内容 本文源自于Quora网站的一个问题,作者称最近一直在为一个新的Rails项目寻找一个JavaScript框架,通过筛选,最终纠结于Angular. ...

  3. 我所改造的JSocket适用于任何DELPHI版本

    JSOCKET是异步选择模式的通信控件,简单而强大,传奇的早期版本就是使用它作通信. { ******************************************************* ...

  4. POJ1185状态压缩DP

    难得的中文题. POJ1185http://poj.org/problem?id=1185 方法就是用DP[i][r][p]表示第i行状态为r,第i-1行状态是p时的最多个数.而这里p受到r的限制,而 ...

  5. AcceptEx与WSAEventSelect和Accept

    (转自论坛的一个帖子http://bbs.csdn.net/topics/280032853) AcceptEx主要用于向完成端口 投递一个或多个的连接请求..当有连接时进来,这里分两种情况: 1.A ...

  6. How Tomcat Works(十一)

    本文接下来分析tomcat的类载入器,tomcat需要实现一个自定义的载入器,而不能使用系统类载入器 (1)限制serlvet访问当前运行的java虚拟机中环境变量CLASSPATH指明的路径下的所有 ...

  7. Xen入门系列一【使用Xen4CentOS 在 Centos 6 上安装 Xen】

    最近在学习Hadoop,在Win7下用VMware搭了三台虚拟机好不容易装好了Hadoop结果跑个两个单词的wordcount就跑了十分钟,郁闷啊,于是开始寻找效能更好的虚拟化解决方案,然后选定了Xe ...

  8. HDU 2437 Jerboas (剪枝搜索)

    题意:给定一幅图,图上有两种点T,P.......一只跳鼠在一个T点作为起始点,它想通过图上的路到达某个P点,P点满足如下要求: (1).到达P点的途中路径权值为k的倍数 (2).尽量让路径权值取最小 ...

  9. MVC6与Asp.net5

    http://www.cnblogs.com/n-pei/p/4263148.html https://blogs.msdn.microsoft.com/scottgu/2015/04/30/asp- ...

  10. 转载:rebar和erlang

    使用rebar生成erlang release 并进行热代码升级 http://blog.sina.com.cn/s/blog_6530ad590100wmkn.html 使用rebar工具开发erl ...