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. TPU使用说明

    1 TPU分类和收费标准 1.1 分类和计费说明 地区 抢占式TPU Cloud TPU 美国 $1.35/hour $4.5/hour 欧洲 $1.485/hour $4.95/hour 亚太区地区 ...

  2. [转载]Meta Learning单排小教学

    原文链接:Meta Learning单排小教学 虽然Meta Learning现在已经非常火了,但是还有很多小伙伴对于Meta Learning不是特别理解.考虑到我的这个AI游乐场将充斥着Meta ...

  3. python-----短信、电话告警

    Twilio 1.访问http://twilio.com/并注册.注册了新账户后,你需要验证一个手机号码,短信将发给该号码. 2.Twilio 提供的试用账户包括一个电话号码,它将作为短信的发送者.你 ...

  4. RabbitMQ简单应用の消息持久化

    消息应答: 消息应答默认是打开的(false) boolean autoAck = true;自动确认模式,表示一旦MQ将消息给到消费者,就会从内存中删除该条消息,这种情况下,如果杀死正在执行的消费者 ...

  5. RabbitMQ四种交换机类型介绍

    RabbitMQ  原文地址: https://baijiahao.baidu.com/s?id=1577456875919174629&wfr=spider&for=pc 最新版本的 ...

  6. 【转】python模块导入细节

    [转]python模块导入细节 python模块导入细节 官方手册:https://docs.python.org/3/tutorial/modules.html 可执行文件和模块 python源代码 ...

  7. python中的正则表达式--re模块

    参考博客:https://www.cnblogs.com/tina-python/p/5508402.html 这里说一下python的re模块即正则表达式模块,先列出其中涉及到的各种字符和模式等: ...

  8. PHP JSON 数据解析代码

    作者: 字体:[增加减小] 类型:转载   PHP解析JSON 数据代码,与大多数流行的 Web 服务如 twitter .人人网通过开放 API 来提供数据一样,它总是能够知道如何解析 API 数据 ...

  9. 题解-USACO18DEC Sort It Out

    Problem 洛谷5156 题意概要:给定一个长为\(n\)的排列,可以选择一个集合\(S\)使这个集合内部元素排到自己在整个序列中应该在的位置(即对于集合\(S\)内的每一个元素\(i\),使其排 ...

  10. SpringCloud的版本

    Spring Cloud 项目目前仍然是快速迭代期,版本变化很快.这里整理一下版本相关的东西,备忘一下. 大版本 版本号规则 Spring Cloud并没有熟悉的数字版本号,而是对应一个开发代号. C ...