//	hdu 1671 Phone List 字典树
//
// 题目大意:
//
// 有一些电话号码的字符串长度最多是10,问是否存在字符串是其它字符串的前缀
//
//
// 解题思路:
//
// 字典树,先插入第一个字符串,然后依照查询,插入的方式进行訪问,发现了之后
// 就不用再进行字典树的操作了
//
//
// 感悟:
//
// 题目意思非常清楚,我在细节方面思考了非常久,插入方面在每一个串的根节点的时候加
// 上一个val值标记。查询的时候先看是否有标记,有则直接返回true。没找到返回
// false。找到了返回true即可了,还是非常easy的~COME ON ! FIGHTING~ #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream> using namespace std; const int MAX_N = 300000; int n; struct Trie {
int ch[MAX_N][11];
int sz;
int val[MAX_N]; int idx(char c){
return c - '0';
} void init(){
sz = 1;
memset(ch[0],0,sizeof(ch[0]));
val[0] = 0;
} void insert(char *s,int v){
int n = strlen(s);
int u = 0; 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] = v; } bool query(char *s){
int n = strlen(s);
int u = 0; for (int i=0;i<n;i++){
int c = idx(s[i]);
//cout << s[i]; if (val[u]){
return true;
} if (!ch[u][c]){
return false;
} u = ch[u][c];
}
return true;
} }trie; void input(){
trie.init();
scanf("%d",&n);
char s[20];
scanf("%s",s);
trie.insert(s,1); bool flag = false;
for (int i=2;i<=n;i++){
scanf("%s",s);
if (flag)
continue;
flag = trie.query(s);
//cout << endl;
trie.insert(s,i);
//cout << i << endl;
}
if (flag)
puts("NO");
else
puts("YES");
} int main(){
int t;
//freopen("1.txt","r",stdin);
scanf("%d",&t);
while(t--){
input();
}
}

hdu 1671 Phone List 字典树的更多相关文章

  1. [ACM] hdu 1671 Phone List (字典树)

    Phone List Problem Description Given a list of phone numbers, determine if it is consistent in the s ...

  2. hdu 1671 Phone List 字典树模板

    Given a list of phone numbers, determine if it is consistent in the sense that no number is the pref ...

  3. hdu 1251 统计难题 (字典树入门题)

    /******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...

  4. HDU 5536 Chip Factory 字典树

    Chip Factory Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...

  5. HDU 1298 T9(字典树+dfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1298 题意:模拟手机9键,给出每个单词的使用频率.现在给出按键的顺序,问每次按键后首字是什么(也就是要概率最大的 ...

  6. HDU 2846 Repository(字典树,每个子串建树,*s的使用)

    Repository Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  7. hdu 1251 统计难题(字典树)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others) Total Subm ...

  8. HDU 1298 T9【字典树增加||查询】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=1298 T9 Time Limit: 2000/1000 MS (Java/Others)    Memo ...

  9. hdu 1251 统计难题 (字典树(Trie)<PS:C++提交不得爆内存>)

    统计难题Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submis ...

随机推荐

  1. Web性能测试术语

    并发用户: 并发一般分为2种情况.一种是严格意义上的并发,即所有的用户在同一时刻做同一件事情或者操作,这种操作一般指做同一类型的业务.比如在信用卡审批业 务中,一定数目的用户在同一时刻对已经完成的审批 ...

  2. Application received signal SIGSEGV

    Application received signal SIGSEGV (null) (( 0 CoreFoundation 0x0000000181037d50 <redacted> + ...

  3. Redis系列(十一)--阿里云开发规范

    本文主要介绍阿里云Redis的开发规范,主要从以下几个方面说明: 1.键值设计 2.命令使用 3.客户端使用 4.相关工具 一.键值设计 1.key name设计 1).[建议]:可读性和可管理性 以 ...

  4. bzoj 4026 dC Loves Number Theory 主席树+欧拉函数

    题目描述 dC 在秒了BZOJ 上所有的数论题后,感觉萌萌哒,想出了这么一道水题,来拯救日益枯竭的水题资源.给定一个长度为 n的正整数序列A,有q次询问,每次询问一段区间内所有元素乘积的φ(φ(n)代 ...

  5. Gym - 101670E Forest Picture (CTU Open Contest 2017 模拟)

    题目: https://cn.vjudge.net/problem/1451310/origin 题意&思路: 纯粹模拟. 大体题意是这样的: 1.有人要在一个10-9<=x<=1 ...

  6. virtualenv与virtualenvwrapper

    一.Linux下安装.配置virtualenv pip3 install virtualenv # 创建虚拟环境env1 virtualenv env1 --no-site-packages --py ...

  7. keepalived(nginx的高可用)安装文档

    1. 安装环境 su - root yum -y install kernel-devel* yum -y install openssl-* yum -y install popt-devel yu ...

  8. Python习题之列表排序,4种方法

    def sort_list_method_1(a): return sorted(a) print(sort_list_method_1([1, 4, 2])) def sort_list_metho ...

  9. 关于React.PropTypes的废除,以及新版本下的react的验证方式

    React.PropTypes是React用来typechecking的一个属性.要在组件的props上运行typechecking,可以分配特殊的propTypes属性: class Greetin ...

  10. 洛谷月赛2018.8 T1题解(U28036 Nagisa loves Tomoya)

    [题解] 我们设原来的数组为a1,a2,a3..., 那么一次操作之后的数组变为a1+a2,a2+a3,a3+a4..., 两次操作之后数组变为a1+2a2+a3,a2+2a3+a4,a3+2a4+a ...