pid=1671">Phone List

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 12879    Accepted Submission(s): 4391

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

解析:求是否为非前缀串(即随意串的前缀都不是一个已有的串)。在建Trie树时,仅仅标记串的最后一个字符,然后查询时,看每一个串的中间是否存在被标记的点就可以。

AC代码:

#include <bits/stdc++.h>
using namespace std; const int maxnode = 100000 * 10 + 5;
const int sigma_size = 10; struct Trie{
int ch[maxnode][sigma_size];
int val[maxnode];
int sz; void clear(){ sz = 1; memset(ch[0], 0, sizeof(ch[0])); } //初始仅仅有一个根节点
int idx(char c){ return c - '0'; } void insert(string s){
int u = 0, n = s.size();
for(int i=0; i<n; i++){
int c = idx(s[i]);
if(!ch[u][c]){
memset(ch[sz], 0, sizeof(ch[sz]));
val[sz] = 0;
ch[u][c] = sz++;
}
u = ch[u][c];
}
val[u] ++; //仅仅标记串尾的字符
} bool find(string s){
int u = 0, n = s.size();
for(int i=0; i<n; i++){
int c = idx(s[i]);
if(val[u]) return false; //在一个串的中间出现了已经标记的点
u = ch[u][c];
}
return true;
} }; Trie T;
vector<string> S; int main(){
#ifdef sxk
freopen("in.txt", "r", stdin);
#endif // sxk int t, n;
string s;
scanf("%d", &t);
while(t--){
S.clear();
T.clear(); //不要忘了初始化
scanf("%d", &n);
for(int i=0; i<n; i++){
cin>>s;
T.insert(s);
S.push_back(s);
}
int cnt = S.size();
int i;
for(i=0; i<cnt; i++){
if(!T.find(S[i])) break;
}
puts(i < cnt ? "NO" : "YES");
}
return 0;
}

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

  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 2846:Repository(Trie)

    http://acm.hdu.edu.cn/showproblem.php?pid=2846 题意:给出N个模式串,再给出M个文本串,问每一个文本串在多少个模式串中出现. 思路:平时都是找前缀的,这里 ...

  3. HDU 1251-统计难题(Trie)

    题意: 给一组单词 开始提问每次给一个串求该串是上面几个单词的前缀 分析: 没给数据规模,但用链表写ME好几次,又用数组写开小RE了,试了几次才过了,真是醉了... #include <map& ...

  4. HDU 1251 统计难题 (Trie)

    pid=1251">统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/ ...

  5. HDU 5938 Four Operations(四则运算)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  6. HDU 5775 Bubble Sort(冒泡排序)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  7. HDU 1711 Number Sequence(数列)

    HDU 1711 Number Sequence(数列) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...

  8. HDU 1005 Number Sequence(数列)

    HDU 1005 Number Sequence(数列) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...

  9. HDU 1176 免费馅饼 (动态规划)

    HDU 1176 免费馅饼 (动态规划) Description 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy的人品实在是太好了,这馅饼 ...

随机推荐

  1. 混合app

    cordova run android            把应用发送到手机   ionic serve        电脑浏览器调试命令   创建: cordova create hello co ...

  2. HTML DOM应用案例2

    <html> <head> <title>day03</title> <script type="text/javascript&quo ...

  3. PTA 循环单链表区间删除 (15 分)

    本题要求实现带头结点的循环单链表的创建和单链表的区间删除.L是一个带头结点的循环单链表,函数ListCreate_CL用于创建一个循环单链表,函数ListDelete_CL用于删除取值大于min小于m ...

  4. Red Hat 7.0 DNS服务配置笔记

    先挂载镜像,然后配置yum,然后安装yum install -y bind 配置静态 IP.DNS就是他本身的IP地址. 修改DNS的配置文件,在后面加入区域配置信息.vim /etc/named.c ...

  5. java 中 针对数组进行的工具类

    1.遍历数组的方法: public static void printfArray(int[] arr)  2. 获取数组中最大值: public static int getMax(int[] ar ...

  6. C#中SQL语句参数写法

    OracleConnection oc=new OracleConnection("data source=osserver;User Id=****;password=**"); ...

  7. Java多线程学习之Lock与ReentranLock详解

    synchronized 是内置锁,而Lock 接口定义的是显示锁,Lock 提供了一种可重入的.可轮询的.定时的以及可中断的锁获取操作. ReenTranLock实现了Lock接口,并提供了与syn ...

  8. IE6常见CSS解析Bug及hack

    IE6常见CSS兼容问题总结 1)图片间隙 A)div中的图片间隙(该bug出现在IE6及更低版本中) 描述:在div中插入图片时,图片会将div下方撑大三像素. hack1:将</div> ...

  9. Google Python编程规范

    http://pan.baidu.com/s/1dD1Ra7J 其他语言的编程风格: http://zh-google-styleguide.readthedocs.org/en/latest/

  10. java获取泛型信息

    总结一下java中获取与泛型相关的信息的知识,不如说是使用方法.网上也有很多类似的优秀文章,这里主要做一个知识的总结.通过反射获取泛型信息的常见例子: //bean package testProje ...