Phone List

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 25724    Accepted Submission(s): 8593

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

题意:判断每个样例输入的电话号码中是否有号码是其它号码的前缀

法1、将输入的几串号码排序,然后判断是否有一串号码是否是后面一串号码的前缀即

可。

法2、建立字典树,查询到单词尾部并且访问节点访问次数>1(因为要去除本身的情

况),即可证明该单词是其他单词的前缀(切记释放内存);

#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
char s[10001][11];
int cmp(const void *m,const void *n)
{
return strcmp((char*) m,(char*) n);//n和m倒过来即为降序
}
int main()
{
int T,n,flag;
scanf("%d",&T);
while(T--)
{
flag=0;
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%s",s[i]);
qsort(s,n,sizeof(s[0]),cmp);//升序排序
//sizeof(s[0])是一个元素的大小,所有元素大小都一样
for(int i=0;i<n;i++){
if(!strncmp(s[i],s[i+1],strlen(s[i]))){
flag=1;break;
}
}
/*int strcmp(const char *s1,const char * s2); 比较s1,s2二个字符串的大小.
int strncmp(char *str1, char *str2, int maxlen); 比较s1,s2二个字符串,
前maxlen字符的大小。当maxlen为s1,s2中字符串最长长度时,相当
于strcmp.*/
if(flag)
printf("NO\n" );
else printf("YES\n");
}
return 0;
}
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
char s[11111][11];
struct node
{
int cnt;//记录节点访问次数
struct node *next[10];
node()
{
for(int i=0;i<=9;i++)
next[i]=NULL;
cnt=0;
}
}*rt;
void insert(char *ss)
{
int x;
node *p=rt;
node *tem=NULL;
for(int i=0;ss[i];i++)
{
x=ss[i]-'0';
if(p->next[x]==NULL)
tem=new node,p->next[x]=tem;
p=p->next[x];
p->cnt++;
}
}
bool check(node *p)
{
for(int i=0;i<10;i++)
if(p->next[i])
return 1;
return 0;
}
bool find1(char *ss)
{
int x;
node *p=rt;
for(int i=0;ss[i];i++)
{
x=ss[i]-'0';
if(!p->next[x])
return 0;
p=p->next[x];
}
if(p->cnt>1)return 1;//访问过不止一次即可证明情况存在
else return 0;
}
void del(node *root)
{
for(int i=0;i<10;i++)
{
if(root->next[i]!=NULL)
{
del(root->next[i]);
}
}
free(root);//释放
}
int main()
{
int T,n,flag;
scanf("%d",&T);
while(T--)
{
flag=0;
rt=new node;//必须在内部,否则超限
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%s",s[i]),insert(s[i]);
for(int i=0;i<n;i++)
if(find1(s[i])){
flag=1;break;
}
if(flag)
printf("NO\n");
else
printf("YES\n");
del(rt);//防止内存超限
}
return 0;
}

HDU 1671 Phone List (qsort字符串排序与strncmp的使用 /字典树)的更多相关文章

  1. hdu 1671 Phone List 字典树

    // hdu 1671 Phone List 字典树 // // 题目大意: // // 有一些电话号码的字符串长度最多是10,问是否存在字符串是其它字符串的前缀 // // // 解题思路: // ...

  2. HDU 1671 Phone List(Trie的应用与内存释放)

    Phone List Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  3. OpenJudge计算概论-字符串排序

    /*====================================================================== 字符串排序 总时间限制: 1000ms 内存限制: 6 ...

  4. Java基础知识强化之IO流笔记52:IO流练习之 把一个文件中的字符串排序后再写入另一个文件案例

    1. 把一个文件中的字符串排序后再写入另一个文件 已知s.txt文件中有这样的一个字符串:"hcexfgijkamdnoqrzstuvwybpl" 请编写程序读取数据内容,把数据排 ...

  5. Trie树|字典树(字符串排序)

    有时,我们会碰到对字符串的排序,若采用一些经典的排序算法,则时间复杂度一般为O(n*lgn),但若采用Trie树,则时间复杂度仅为O(n). Trie树又名字典树,从字面意思即可理解,这种树的结构像英 ...

  6. Openjudge-计算概论(A)-字符串排序

    描述 参考整数排序方法,设计一种为字符串排序的算法,将字符串从小到大输出 输入 第一行为测试数据组数t, 后面跟着t组数据.每组数据第一行是n,表示这组数据有n行字符串,接下来是要排序的n行字符串.每 ...

  7. [C]字符串排序之-冒泡法

    在oj刷题,遇见一题字符串排序题. 脑海里瞬间闪过数组排序. 思路有了,打开题解看看别人的思路,发现好多人的排序方法显得比较臃肿,可能也是我的水平不够,欣赏不来吧. 不过用冒泡法排序的时候一定要记得字 ...

  8. 51 nod 1097 拼成最小的数 思路:字符串排序

    题目: 思路:1.以字符串输入这些整数. 2.对这些字符串排序,排序规则为尽量让能让结果变小的靠前. 代码中有注释,不懂的欢迎在博客中评论问我. 代码: #include <bits\stdc+ ...

  9. Mysql Order By 字符串排序,mysql 字符串order by

    Mysql Order By 字符串排序,mysql 字符串order by ============================== ©Copyright 蕃薯耀 2017年9月30日 http ...

随机推荐

  1. Spring Boot:如何配置静态资源的地址与访问路径

    spring.resources.static-locations=classpath:/static,classpath:/public,classpath:/resources,classpath ...

  2. 20165231 2017-2018-2 《Java程序设计》第6周学习总结

    教材学习内容总结 第八章 String类 Java专门提供了用来处理字符序列的String类. String类在java.lang包中,由于java.lang包中的类被默认引入,因此程序可以直接使用S ...

  3. Pytorch复现Faster-RCNN

    5月的最后一天,需要写点什么. 通过前几篇博客对Faster-RCNN算是有了一个比较全面的认识,接下来的半个月断断续续写了一些代码,基本上复现了论文.利用torchvision的VGG16预训练权重 ...

  4. vc++高级班之多线程篇[7]---线程间的同步机制②

    //示例代码: CStringArray g_ArrString; UINT __cdecl ThreadProc(LPVOID lpParameter) {  int startIdx = (int ...

  5. MYSQL添加远程用户或允许远程访问

    1.用root用户登陆 格式:grant 权限 on 数据库教程名.表名 to 用户@登录主机 identified by "用户密码";             @ 后面是访问M ...

  6. 【转】vector中erase()的使用注意事项

    vector::erase():从指定容器删除指定位置的元素或某段范围内的元素 vector::erase()方法有两种重载形式 如下: iterator erase(   iterator _Whe ...

  7. ES--04

    第三十一讲! 分布式文档系统 写一致性原理以及相关参数 课程大纲 (1)consistency,one(primary shard),all(all shard),quorum(default) 我们 ...

  8. pointer & iterator

    pointer: address, use operator(*) to get/set the value 1) support operator(+,-), move to next posito ...

  9. RocketMQ实战快速入门

    转自:https://www.jianshu.com/p/824066d70da8 一.RocketMQ 是什么      Github 上关于 RocketMQ 的介绍:RcoketMQ 是一款低延 ...

  10. Golang 类型转换,断言和显式强制转换

    1 前言 类型转换,可以用断言(只能使用在interface{}类型转换成其它类型)和显式类型强制转换(常规是用于基本类型) 2 代码 //graphql-go func(params graphql ...