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. 用多线程处理FTP上传

    在开发中遇到总站发送命令请求分站将某资源通过FTP上传过来,也就是总站提取分站的资源问题.并且总站实时可以获取已经提取了文件的大小的比例. 思路:1.首先分站要将文件大小告知总站 2.总站收到文件大小 ...

  2. 如何在vue中使用动态使用本地图片路径

    不知道各位小伙伴有没有在开发遇到一个问题,就是在线上的项目使用后台返回本地图片路径,然后加载不上的情况呢? 我的解决方法就是:先在项目的data下定义好这样一个数组用于存放需要加载的路径 [ {nam ...

  3. matplotlib 直方图绘制详解

    n, bins, patches = plt.hist(datasets, bins, normed=False, facecolor=None, alpha=None) 函数说明 用于绘制多个数据集 ...

  4. Linker Scripts3--链接脚本概述

    1.前言 本文主要翻译了The Link Script英文文献. (1)每个链接都是由链接脚本控制,链接脚本是用链接命令语言写的: (2)链接脚本的主要目的是描述输入文件的sections如何映射到输 ...

  5. HTTP笔记01-http相关的基础知识

    这个系列文章是阅读<图解HTTP>后写下的笔记 当我们在浏览器输入url,点击回车后,浏览器显示我们需要的web页面,那么,这个界面是如何产生的? 根据浏览器地址中输入的url,浏览器从相 ...

  6. hibernate框架学习之增删改查helloworld

    插入数据删除数据修改数据查询单条数据查询多条数据 HelloWorldApp.java package cn.itcast.h3.helloworld; import org.hibernate.Se ...

  7. MySQL--表操作(innodb表字段数据类型、约束条件)、sql_mode操作

    一.创建表的完整语法 #[]内的可有可无,即创建表时字段名和类型是必须填写的,宽度与约束条件是可选择填写的.create table 表名(字段名1 类型[(宽度) 约束条件],字段名2 类型[(宽度 ...

  8. MySQL新增多个字段

    alter table pic_all add ( `expand1` ), `expand2` ), `expand3` ) );

  9. Linux mem/swap/buffers/cached 区别

    Free free 命令相对于top 提供了更简洁的查看系统内存使用情况: $ free total used free shared buffers cached Mem: 255268 23833 ...

  10. 快速解决PHP调用Word组件DCOM权限的问题

    1. 首先必须要在电脑上安装 Office 2. windows+r : 输入 dcomcnfg.exe 打开组件服务,然后双击 组件服务 ==> 双击 计算机 ==> 双击 我的电脑 = ...