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. index structure

    1. wordlist 0) 0, 1byte 1) token-id(delta), 8byte 2) doclist-offset(delta), 8byte 3) doc_count, 4byt ...

  2. Cisco Router WEB管理

    目前市场上很多思科路由器或者交换机都可以通过WEB方式配置.尽管很多功能还是只能通过CLI配置,但是一些功能还是很有用的,例如端口的流量监控功能 前期准备: 一.设备的IOS要支持WEB管理功能   ...

  3. 转载sublime text3中package Control

    Sublime Text 3 安装Package Control 原来Subl3安装Package Control很麻烦,现在简单的方法来了 一.简单的安装方法 使用Ctrl+`快捷键或者通过View ...

  4. Lotus 迁移到Exchange POC 之 新建2007 服务器!

    我们登录到Exchange 2007 服务器,由于需要对AD进行扩展,我们首先必须完成架构扩展,由于默认没有ldifde工具,所以我们需要执行servermanagercmd –I rsat-adds ...

  5. My集合框架第四弹 HashTable(链表解决冲突)

    package com.wpr.collection; import java.util.LinkedList; import java.util.List; public class HashTab ...

  6. Unity3d:编辑器中运行正常,发布后的exe提示找不到文件

    解决方案1:查看文件路径拼写方式,如果是用“+”拼接的,请改用System.IO.Path.Combine()方式拼接.经过测试,两种拼接方式打印出来的路径是一样的,但为什么 加号 的方式拼接unit ...

  7. android 检测ListView滚动到的位置

    ListView滚动 1.要用到一个监听事件是:setOnScrollListener(); API解释是: Set the listener that will receive notificati ...

  8. Python常用网页字符串处理技巧

    首先一些Python字符串处理的简易常用的用法.其他的以后用到再补充. 1.去掉重复空格 s = "hello hello hello" s = ' '.join(s.split( ...

  9. Keil AGDI Header File

    #ifndef __AGDI__INCED___ #define __AGDI__INCED___ //---Revision History: --------------------------- ...

  10. PPAS上运行pg_dump经过

    目前我有两台机器, 分别已经安装了PPAS9.1,安装后建立了OS系统用户enterprisedb和数据库用户enterprisedb. 机器1:master  192.168.10.88 机器2: ...