hdu 1671 Phone List 字典树
// 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 字典树的更多相关文章
- [ACM] hdu 1671 Phone List (字典树)
Phone List Problem Description Given a list of phone numbers, determine if it is consistent in the s ...
- hdu 1671 Phone List 字典树模板
Given a list of phone numbers, determine if it is consistent in the sense that no number is the pref ...
- hdu 1251 统计难题 (字典树入门题)
/******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...
- HDU 5536 Chip Factory 字典树
Chip Factory Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...
- HDU 1298 T9(字典树+dfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1298 题意:模拟手机9键,给出每个单词的使用频率.现在给出按键的顺序,问每次按键后首字是什么(也就是要概率最大的 ...
- HDU 2846 Repository(字典树,每个子串建树,*s的使用)
Repository Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- hdu 1251 统计难题(字典树)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Total Subm ...
- HDU 1298 T9【字典树增加||查询】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=1298 T9 Time Limit: 2000/1000 MS (Java/Others) Memo ...
- hdu 1251 统计难题 (字典树(Trie)<PS:C++提交不得爆内存>)
统计难题Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submis ...
随机推荐
- RPC——笔记
整理的笔记来源:https://mp.weixin.qq.com/s/JkXrPcuKtE2qYgmDcH2uww RPC(远程过程调用): RPC是:一个计算机通信协议. 调用过程:计算机 A 上的 ...
- 【Python-2.7】大小写转换函数
字母大小写是编程过程中经常遇到的问题,如下函数可以灵活的进行大小写转换: title():把单词首字母转换为大写: upper():把每个字母转换为大写: lower():把每个字母转换为小写. 示例 ...
- 如何实现ADSL宽带用户开机自动拨号与定时拨号
在宽带拨号网络的环境下,要通过手动拨号认证才能上网.下面给大家介绍怎么设置开机自动拨号上网以及定时拨号上网. 这也是为一个叫CHY的2B准备的技术套餐,不需要用到网上说的自动拨号软件,只要在主机上设置 ...
- (转)WKT转换工具terraformers
http://blog.csdn.net/gisshixisheng/article/details/53150111 概述: 前面的文章中,提到了Arcgis中实现wkt转换为geometry,但是 ...
- Leetcode加一 (java、python3)
加一 给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储一个数字. 你可以假设除了整数 0 之外,这个整数不会以零开头. Given ...
- Queueingconsumer 找不到
springboot从1.5.9升级到2.0.0,queueingconsumer报错没有这个类,改为使用 DefaultConsumer
- 【实验级】Docker-Compose搭建单服务器ELK伪集群
本文说明 由于最近在搭ELK的日志系统,为了演示方案搭了个单台服务器的日志系统,就是前一篇文章中所记,其实这些笔记已经整理好久了,一直在解决各种问题就没有发出来.在演示过程中我提到了两个方案,其中之一 ...
- MyBatis 创建核心配置文件和 SQL 映射文件
Mybatis 的两个配置文件(mybatis-config.xml 和 xxxMapper.xml)都为 xml 类型,因此在 eclipse 中创建 xml 文件命名为相应的 mybatis-c ...
- Random和ArrayList的应用
/*Random类应用与Math类应用,创建一个类, * 1)分别用Random类和Math.random()方法生成随机数. * 2) 把Math.random()方法生成的随机数,转换成1-100 ...
- react入门----事件监听
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...