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(字典树)

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

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

    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.7.3移除字符串中重复字符(一)

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

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

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

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

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

  8. HDU 3336 输出包括从1到len长 字符串前缀的总个数(+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. 食物链--poj1182(并查集含有关系)

    http://poj.org/problem?id=1182   题意应该就不用说了  再次回到食物链这道题,自己写了一遍,一直wa...原因竟然是不能用多实例,我也是醉了,但是我真的彻底的理解了,那 ...

  2. HTTP 协议详解(转载)

    原文: http://kb.cnblogs.com/page/130970/#httpmeessagestructe HTTP协议详解 当今web程序的开发技术真是百家争鸣,ASP.NET, PHP, ...

  3. Java泛型初探

    概述 泛型就是参数化类型,一提到参数,最熟悉的就是定义方法时有形参,然后调用此方法时传递实参,其实你回味一下这里.形参参数化的是变量的值,而如果你想参数化变量的类型,那就用到泛型了.同样的, 定义的时 ...

  4. python 全局变量引用与修改

    一.引用 使用到的全局变量只是作为引用,不在函数中修改它的值的话,不需要加global关键字.如: #! /usr/bin/python a = 1 b = [2, 3] def func(): if ...

  5. 几种常用CSS3样式

    在我们日常工作中,由于考虑到浏览器的兼容性,所以很少用CSS3样式.关于其标准,W3C 仍然在对 CSS3 规范进行开发.不过,现代浏览器已经实现了相当多的 CSS3 属性.最近学习了CSS3,发现功 ...

  6. 在windows中:双击运行Python程序、后台运行Python程序

    在windows中:双击运行Python程序.后台运行Python程序 安装Python解释器的windows环境,如果双击运行*.py的文件,会闪退.怎样避免闪退呢? 我们用python的日志输出程 ...

  7. SQL SERVER 聚集索引 非聚集索引 区别

    转自http://blog.csdn.net/single_wolf_wolf/article/details/52915862 一.理解索引的结构 索引在数据库中的作用类似于目录在书籍中的作用,用来 ...

  8. dedecms手机站图片错误的解决方法

    现在手机站(如m.*.com)是网站建设标配,在用dedecms建站也可以实现(不会的话欢迎来骚扰ytkah),手机站一个比较麻烦的事是图片一直显示不出来,为什么呢?程序一般是调用图片的相对地址,比如 ...

  9. wordpress注册后重定向到自定义页面

    wordpress注册后重定向到自定义页面怎么操作?将下面的代码添加到当前主题的 functions.php 文件中即可 add_filter( 'registration_redirect', 'w ...

  10. Agreementhasbeenupdated--EditPhoneNumber

    1.https://appleid.apple.com/#!&page=signin 登录appid账号  在账户里面的编辑 添加更多信息,新增手机号 2.如果不可以 可以试下这个https: ...