HDU 1671 Phone List (qsort字符串排序与strncmp的使用 /字典树)
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的使用 /字典树)的更多相关文章
- hdu 1671 Phone List 字典树
// hdu 1671 Phone List 字典树 // // 题目大意: // // 有一些电话号码的字符串长度最多是10,问是否存在字符串是其它字符串的前缀 // // // 解题思路: // ...
- HDU 1671 Phone List(Trie的应用与内存释放)
Phone List Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- OpenJudge计算概论-字符串排序
/*====================================================================== 字符串排序 总时间限制: 1000ms 内存限制: 6 ...
- Java基础知识强化之IO流笔记52:IO流练习之 把一个文件中的字符串排序后再写入另一个文件案例
1. 把一个文件中的字符串排序后再写入另一个文件 已知s.txt文件中有这样的一个字符串:"hcexfgijkamdnoqrzstuvwybpl" 请编写程序读取数据内容,把数据排 ...
- Trie树|字典树(字符串排序)
有时,我们会碰到对字符串的排序,若采用一些经典的排序算法,则时间复杂度一般为O(n*lgn),但若采用Trie树,则时间复杂度仅为O(n). Trie树又名字典树,从字面意思即可理解,这种树的结构像英 ...
- Openjudge-计算概论(A)-字符串排序
描述 参考整数排序方法,设计一种为字符串排序的算法,将字符串从小到大输出 输入 第一行为测试数据组数t, 后面跟着t组数据.每组数据第一行是n,表示这组数据有n行字符串,接下来是要排序的n行字符串.每 ...
- [C]字符串排序之-冒泡法
在oj刷题,遇见一题字符串排序题. 脑海里瞬间闪过数组排序. 思路有了,打开题解看看别人的思路,发现好多人的排序方法显得比较臃肿,可能也是我的水平不够,欣赏不来吧. 不过用冒泡法排序的时候一定要记得字 ...
- 51 nod 1097 拼成最小的数 思路:字符串排序
题目: 思路:1.以字符串输入这些整数. 2.对这些字符串排序,排序规则为尽量让能让结果变小的靠前. 代码中有注释,不懂的欢迎在博客中评论问我. 代码: #include <bits\stdc+ ...
- Mysql Order By 字符串排序,mysql 字符串order by
Mysql Order By 字符串排序,mysql 字符串order by ============================== ©Copyright 蕃薯耀 2017年9月30日 http ...
随机推荐
- SQL Server - SQL语言组成 (DML、DDL、DCL、DQL的区别)
转载自:https://www.cnblogs.com/dato/p/7049343.html 作者:Dato.zeng --------------------------------------- ...
- 修改Spring Social默认提交地址
⒈ package cn.coreqi.social.config; import org.springframework.social.security.SocialAuthenticationFi ...
- 数字图像处理的Matlab实现(3)—灰度变换与空间滤波
第3章 灰度变换与空间滤波(1) 3.1 简介 空间域指的是图像平面本身,这类方法是以对图像像素直接处理为基础的.本章主要讨论两种空间域处理方法:亮度(灰度)变换与空间滤波.后一种方法有时涉及到邻域处 ...
- 统计分析与R软件-chapter2-5
2.5 多维数组和矩阵 2.5.1 生成数组或矩阵 数组有一个特征属性叫做维数向量(dim属性),维数向量是一个元素取正整数的向量,其长度是数组的维数,比如维数向量有两个元素时数组为2维数组(矩阵). ...
- 统计分析与R软件-chapter2-4
2.4 因子 统计中的变量有几中重要类别:区间变量.名义变量和有序变量.区间变量取连续的数值,可以进行求和.平均值等运算.名义变量和有序变量取离散值,可以用数值代表,也可以是字符型值,其具体数值没有加 ...
- Pftriage:分析和追踪恶意文件,识别特征
项目地址 PFTriage:https://github.com/idiom/pftriage 参考 Pftriage:如何在恶意软件传播过程中对恶意文件进行分析 https://www.freebu ...
- Linux 应用层的时间编程【转】
转自:https://blog.csdn.net/chinalj2009/article/details/21223681 浅析 Linux 中的时间编程和实现原理,第 1 部分: Linux 应用层 ...
- python3+requests库框架设计07-生成测试报告
使用HTMLTestRunner可以生成测试报告.HTMLTestRunner是unittest模块下的一个拓展,原生的生成报告样式比较丑,GitHub上有大佬优化过后的版本:GitHub地址.下载之 ...
- Entity framework中LINQ的使用
一.linq和ef的差别 我们做项目时,难免会遇到用的不知道是啥,及把linq和EF搞混了.今天我带领大家梳理下思路,首先说linq查询,然后介绍EF查询 1.linq查询 当我们使用linq查询时, ...
- linux学习之硬盘的存储原理和内部架构
原文地址:https://blog.csdn.net/tanggao1314/article/details/52074735 首先,让我们看一下硬盘的发展史: 1956年9月13日,IBM的IBM ...