Phone List

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 18202    Accepted Submission(s): 6115

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
 
Source
 
 
 
解析:字典树。
 
 
 
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std; struct Node{
Node* pt[10];
bool isEnd;
};
Node* root; int n;
string s[10005];
Node memory[100000];
int allo; void trie_init()
{
memset(memory, 0, sizeof(memory));
allo = 0;
root = &memory[allo++];
} bool trie_insert(string& str)
{
Node *p = root, *q;
for(int i = 0; str[i] != '\0'; ++i){
int id = str[i]-'0';
if(p->pt[id] == NULL){
q = &memory[allo++];
p->pt[id] = q;
}
else{
if(p->pt[id]->isEnd)
return false;
}
p = p->pt[id];
}
p->isEnd = true;
return true;
} void solve()
{
trie_init();
bool ok = true;
for(int i = 0; i < n; ++i){
ok = trie_insert(s[i]);
if(!ok){
cout<<"NO"<<endl;
return;
}
}
cout<<"YES"<<endl;
} bool cmp(const string& a, const string& b)
{
return a.length() < b.length();
} int main()
{
int t;
cin>>t;
while(t--){
cin>>n;
for(int i = 0; i < n; ++i)
cin>>s[i];
sort(s, s+n, cmp);
solve();
}
return 0;
}

  

HDU 1671 Phone List(POJ 3630)的更多相关文章

  1. HDU 1671 Phone List(Trie的应用与内存释放)

    Phone List Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  2. hdu 1671 Phone List 字典树

    // hdu 1671 Phone List 字典树 // // 题目大意: // // 有一些电话号码的字符串长度最多是10,问是否存在字符串是其它字符串的前缀 // // // 解题思路: // ...

  3. POJ 3630 , HDU 1671 Phone List - from lanshui_Yang

    这道题也是一道找前缀的问题,很自然地要用到Trie树,但是如果用动态Trie树(即用指针开辟内存)的话,虽然在HDU上可以过(可能是HDU的数据比较水),但在POJ上会TLE , 所以这道题只能用静态 ...

  4. hdu 1671&& poj 3630 (trie 树应用)

    Phone List Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25280   Accepted: 7678 Descr ...

  5. Hdu 3887 Counting Offspring \ Poj 3321 Apple Tree \BZOJ 1103 [POI2007]大都市meg

    这几个题练习DFS序的一些应用. 问题引入: 给定一颗n(n <= 10^5)个节点的有根树,每个节点标有权值,现有如下两种操作: 1.C x y     以节点x的权值修改为y. 2.Q x ...

  6. POJ 3630 Phone List(trie树的简单应用)

    题目链接:http://poj.org/problem?id=3630 题意:给你多个字符串,如果其中任意两个字符串满足一个是另一个的前缀,那么输出NO,否则输出YES 思路:简单的trie树应用,插 ...

  7. poj 3630 Phone List(字典树)

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

  8. 【POJ 3630】 Phone List

    [题目链接] http://poj.org/problem?id=3630 [算法] 字典树 [代码] #include <algorithm> #include <bitset&g ...

  9. HDU 1671 (字典树统计是否有前缀)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1671 Problem Description Given a list of phone number ...

随机推荐

  1. CentOS安装视频播放器SMPlayer

    首先下载rpmforg,下载对应的版本,就是对应CentOS版本,还有32位与64位也要对应上.地址如下: http://wiki.centos.org/AdditionalResources/Rep ...

  2. javascript背景淡入淡出

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  3. IOS 视图控制对象生命周期-init、viewDidLoad、viewWillAppear、viewDidAppear、viewWillDisappear等的区别及用途

    iOS视图控制对象生命周期-init.viewDidLoad.viewWillAppear.viewDidAppear.viewWillDisappear.viewDidDisappear的区别及用途 ...

  4. .NET framework 4.0安装失败怎么办

    开始——运行——输入cmd——回车——在打开的窗口中输入net stop WuAuServ   开始——运行——输入%windir%找到有个叫SoftwareDistribution的文件夹,把它重命 ...

  5. lintcode:Remove Element 删除元素

    题目: 删除元素 给定一个数组和一个值,在原地删除与值相同的数字,返回新数组的长度. 元素的顺序可以改变,并且对新的数组不会有影响.  样例 给出一个数组 [0,4,4,0,0,2,4,4],和值 4 ...

  6. python 处理 Excel 表格

    see: http://www.cnblogs.com/sunada2005/p/3193300.html 一.可使用的第三方库 python中处理excel表格,常用的库有xlrd(读excel)表 ...

  7. C++Builder和VC的比较

    C++Builder和VC的比较 其实很久以前我就想写这篇文章,其原因一方面是因为笔者深深感觉到C++ Builder的确是一个先进与强大的程式开发工具,但更最重要的一点是,我深信C++ Builde ...

  8. Android:调试之DDMS

    DDMS 的全称是Dalvik Debug Monitor Service,是 Android 开发环境中的Dalvik虚拟机调试监控服务. 在Eclipse,项目启动了虚拟器后,右上角选择Open ...

  9. C++:默认的构造函数

    注意:如果类中用户没有定义构造函数,系统会自动提供一个函数体为空的默认构造函数. 但是,只要类中定义了一个构造函数(不一定无参构造函数),系统将不再给它提供 默认的构造函数.因为,默认的构造函数被类中 ...

  10. shell 数学计算

    每次都找不到一个好的方法来执行shell中的变量计算. 前段时间忘了在哪发现一个好的方法.在此记录下来. 申请变量: value=0; 变量加减: value=$[$value+1] 变量乘除: va ...