POJ 3630 Phone List Trie题解
Trie的应用题目。
本题有两个难点了:
1 动态建立Trie会超时,须要静态建立数组,然后构造树
2 推断的时候注意两种情况: 1) Tire树有133,然后插入13333556的时候。2)插入顺序倒转过来的时候
改动一下标准Trie数的插入函数就能够了:
- #include <stdio.h>
- #include <string.h>
- const int MAX_NODE = 100001;
- const int MAX_WORD = 11;
- const int ARR_SIZE = 10;
- struct Node
- {
- int n;
- Node *arr[ARR_SIZE];
- };
- void clearNode(Node *p)
- {
- p->n = 0;
- for (int i = 0; i < ARR_SIZE; i++)
- {
- p->arr[i] = NULL;
- }
- }
- Node pool[MAX_NODE];
- int poolId;
- bool insertTrie(Node *trie, char nums[])
- {
- Node *pCrawl = trie;
- int len = strlen(nums);
- for (int i = 0; i < len; i++)
- {
- int j = nums[i] - '0';
- //推断1: 情况: Trie有31199,插入311
- if (i + 1 == len && pCrawl->arr[j]) return false;//注意这个easy遗忘条件
- if (!pCrawl->arr[j])
- {
- pCrawl->arr[j] = &pool[poolId++];
- clearNode(pCrawl->arr[j]);
- }
- pCrawl = pCrawl->arr[j];
- if (pCrawl->n) return false;
- }
- for (int i = 0; i < ARR_SIZE; i++)
- {//推断2: 情况: Trie有31199,插入311,和推断1是一样的,删除一个也可。
- if (pCrawl->arr[i]) return false;
- }
- pCrawl->n++;
- return true;
- }
- int main()
- {
- int T, n;
- scanf("%d", &T);
- char word[MAX_WORD];
- Node *trie = &pool[0];
- while (T--)
- {
- clearNode(trie);
- poolId = 1;
- scanf("%d", &n);
- getchar();
- bool consistent = true;
- for (int i = 0; i < n; i++)
- {
- gets(word);
- if (consistent)
- {
- consistent = insertTrie(trie, word);
- }
- }
- if (consistent) puts("YES");
- else puts("NO");
- }
- return 0;
- }
POJ 3630 Phone List Trie题解的更多相关文章
- POJ 3630 Phone List | Trie 树
题目: 给定 n 个长度不超过 10 的数字串,问其中是否存在两个数字串 S, T ,使得 S 是 T 的前缀.多组数据,数据组数不超过 40. 题解: 前缀问题一般都用Trie树解决: 所以跑一个T ...
- poj 3630 Phone List trie树
Phone List Description Given a list of phone numbers, determine if it is consistent in the sense tha ...
- poj 2431 Expedition 贪心 优先队列 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=2431 题解 朴素想法就是dfs 经过该点的时候决定是否加油 中间加了一点剪枝 如果加油次数已经比已知最少的加油次数要大或者等于了 那么就剪 ...
- poj 1064 Cable master 二分 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=1064 题解 二分即可 其实 对于输入与精度计算不是很在行 老是被卡精度 后来学习了一个函数 floor 向负无穷取整 才能ac 代码如下 ...
- POJ 3630 Phone List(trie树的简单应用)
题目链接:http://poj.org/problem?id=3630 题意:给你多个字符串,如果其中任意两个字符串满足一个是另一个的前缀,那么输出NO,否则输出YES 思路:简单的trie树应用,插 ...
- POJ 3630 trie树
Phone List Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26559 Accepted: 8000 Descripti ...
- Phone List POJ 3630 Trie Tree 字典树
Phone List Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 29416 Accepted: 8774 Descr ...
- POJ 3630 Phone List(字符串前缀重复)题解
Description Given a list of phone numbers, determine if it is consistent in the sense that no number ...
- hdu 1671&& poj 3630 (trie 树应用)
Phone List Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 25280 Accepted: 7678 Descr ...
随机推荐
- 怎样配置PHP环境和安装Zendstdio编辑器
想学习PHP好久了.苦于环境配置不好,一直感觉无从下手. 在网上找了个视频: 李炎恢PHP教程 第一章前3节给出了具体的配置的方法,即安装Apache和Zendstudio 10.5仅仅须要照着视频做 ...
- Java-对象多态性
class A { public void fun1() { System.out.println("<----A------>"); } public v ...
- 转载【浅谈ThreadPool 线程池】
浅谈ThreadPool 线程池 http://www.cnblogs.com/xugang/archive/2010/04/20/1716042.html
- Xamarin.forms 自定义tabview控件
一 问题描述 forms本身ui代码是翻译为平台原生代码,forms按照xaml技术进行对android和ios两种ui模型进行公共抽象出了几种page和view,在空杯博客已经有详细介绍 http: ...
- Nginx设置alias实现虚拟目录 alias与root的用法区别
Nginx 貌似没有虚拟目录的说法,因为它本来就是完完全全根据目录来设计并工作的.如果非要给nginx安上一个虚拟目录的说法,那就只有alias标签比较"像",干脆来说说alias ...
- TMsgThread, TCommThread -- 在delphi线程中实现消息循环(105篇博客,好多研究消息的文章)
在delphi线程中实现消息循环 在delphi线程中实现消息循环 Delphi的TThread类使用很方便,但是有时候我们需要在线程类中使用消息循环,delphi没有提供. 花了两天的事件研究了 ...
- 14.2.2 InnoDB Multi-Versioning InnoDB 多版本
14.2.2 InnoDB Multi-Versioning InnoDB 多版本: InnoDB 是一个多版本的存储引擎: 它保留信息关于改变数据的老版本,为了支持事务功能 比如并发和回滚. 这些信 ...
- 单页应用Scrat实践
单页应用Scrat实践 1.开始 随着前端工程化深入研究,前端工程师现在碉堡了,甚至搞了个自己的前端网站http://div.io/需要邀请码才能注册,不过里面的技术确实牛.距离顶级的前端架构,目前博 ...
- 无锁队列--基于linuxkfifo实现
一直想写一个无锁队列,为了提高项目的背景效率. 有机会看到linux核心kfifo.h 原则. 所以这个实现自己仿照,眼下linux我们应该能够提供外部接口. #ifndef _NO_LOCK_QUE ...
- 部署到Linux使用VS Code 开发.NET Core 应用程序
使用VS Code 开发.NET Core 应用程序 部署到Linux 跨平台 使用VS Code 开发.NET Core 应用程序 部署到Linux 跨平台. 前面讲解了VSCode开发调试 .NE ...