POJ 3630 Phone List(字符串前缀重复)题解

时间:2022-11-10 16:07:15

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:

  • Emergency 911
  • Alice 97 625 999
  • 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

思路:

树状数组超时。这里给每一串先排序,然后比较相邻的两个有没有前缀重复就可以了,有点厉害(所以为什么他会出现在树状数组专题呢)。

代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<cmath>
#include<string>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<iostream>
#include<algorithm>
#include<sstream>
#define ll long long
const int N=10005;
const int INF=1e9;
using namespace std;
char s[N][15];
int cmp(const void *a,const void *b){
return strcmp((char *)a,(char *)b);
}
int main(){
int flag,t,n;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%s",s[i]);
}
qsort(s,n,sizeof(s[0]),cmp);
flag=0;
for(int i=0;i<n-1;i++){
if(strncmp(s[i],s[i+1],strlen(s[i]))==0){
flag=1;
break;
}
}
if(flag) printf("NO\n");
else printf("YES\n");
}
return 0;
}

POJ 3630 Phone List(字符串前缀重复)题解的更多相关文章

  1. poj 3630 Phone List&lpar;字典树&rpar;

    题目链接: http://poj.org/problem?id=3630 思路分析: 求在字符串中是否存在某个字符串为另一字符串的前缀: 即对于某个字符串而言,其是否为某个字符串的前缀,或存在某个其先 ...

  2. java-ArrayList中去重复字符串或重复对象、LinkedList集合、泛型、增强for、静态导入、可变参数、asList&lpar;&rpar;方法、集合嵌套

    1.去除ArrayList中重复字符串元素方式 * A:案例演示 * 需求:ArrayList去除集合中字符串的重复值(字符串的内容相同) * 思路:创建新集合方式 /** * A:案例演示 * 需求 ...

  3. python--基础学习(四)自然字符串、重复字符串、子字符串

    python系列均基于python3.4环境 1.自然字符串和重复字符串 代码示例: str1=r'hello \npython' str2='hello \npython' str3="h ...

  4. JS-取出字符串中重复次数最多的字符并输出

    /** 取出字符串中重复字数最多的字符 */ var words = 'sdfghjkfastgbyhnvdstyaujskgfdfhlaa'; //创建字符串 var word, //单个字符 le ...

  5. Python2&period;7&period;3移除字符串中重复字符&lpar;一&rpar;

    移除重复字符很简单,这里是最笨,也是最简单的一种.问题关键是理解排序的意义: # coding=utf-8 #learning at jeapedu in 2013/10/26 #移除给定字符串中重复 ...

  6. javascript 去除字符串中重复字符

    /** * 去除字符串中重复的字符,以下提供2种方法, * removeRepeat()为自己所想: * removeRepeat2()参考网上思路补充的 * removeRepeat3()敬请期待· ...

  7. jst通用删除数组中重复的值和删除字符串中重复的字符

    以下内容属于个人原创,转载请注明出处,非常感谢! 删除数组中重复的值或者删除字符串重复的字符,是我们前端开发人员碰到很多这样的场景.还有求职者在被面试时也会碰到这样的问题!比如:问删除字符串重复的字符 ...

  8. HDU 3336 输出包括从1到len长 字符串前缀的总个数(&plus;DP)

    Sample Input14abab Sample Output6输出包括从1到len长 字符串前缀的总个数abab:包括2个a,2个ab,1个aba,1个abab # include <cst ...

  9. ArrayList去除集合中字符串的重复值

    package com.heima.list; import java.util.ArrayList; import java.util.Iterator; import java.util.List ...

随机推荐

  1. 数据结构与算法之链表-javascript实现

    链表的定义: 链表是一种物理存储单元上非连续.非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的.链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成.每个结点 ...

  2. 转:SAAS 测试

    基于SaaS云计算网络性能测试指标研究 来源:中国软件评测中心  作者:马子明  投稿时间:2011-04-02 1.基于SaaS的云计算 SaaS(Software as a Service,软件即 ...

  3. Deep Learning 6&lowbar;深度学习UFLDL教程:Softmax Regression&lowbar;Exercise(斯坦福大学深度学习教程)

    前言 练习内容:Exercise:Softmax Regression.完成MNIST手写数字数据库中手写数字的识别,即:用6万个已标注数据(即:6万张28*28的图像块(patches)),作训练数 ...

  4. tigerVNC远程桌面,跨内网

    tigerVNC的简单使用教程(CentOS的远程桌面连接) 1.环境和软件准备 (1) CentOS 6.5下 [root@localhost ~]$ yum install tigervnc (2 ...

  5. win7 VMware Workstation Centos6&period;5虚机桥接上网设置 详解(靠谱)

    1.VMware Workstation 设置 2. vim /etc/sysconfig/network-scripts/ifcfg-eth0 NAME="System eth0&quot ...

  6. Spring Cloud Greenwich 正式发布,Hystrix 即将寿终正寝。。

    Spring Cloud Greenwich 正式版在 01/23/2019 这天正式发布了,下面我们来看下有哪些更新内容. 生命周期终止提醒 Spring Cloud Edgware Edgware ...

  7. 【vue】中 &dollar;listeners 的使用方法

    $listeners 的官方介绍: 包含了父作用域中的 (不含 .native 修饰器的) v-on 事件监听器.它可以通过 v-on="$listeners" 传入内部组件——在 ...

  8. 将List的元素通过中文字符串排序

    类customer public class Customer { public String name; public int age; Customer(String name, int age) ...

  9. Golang查缺补漏(一)

    Go语言高级编程(Advanced Go Programming) Go语言高级编程(Advanced Go Programming) golang都是传值,与其他语言不同的是数组作为参数时,也是传值 ...

  10. Kotlin都转正成Android官方语言了,你还不试一下?

    想想Android Studio 和Eclipse ,我觉得你还是有必要入手Kotlin了. 站好队很重要. 以前的一篇总体概括老文,大家可以看看~ 爽翻天!告别Java.一起来使用kotlin开发完 ...