Trie的应用题目。

本题有两个难点了:

1 动态建立Trie会超时,须要静态建立数组,然后构造树

2 推断的时候注意两种情况: 1) Tire树有133,然后插入13333556的时候。2)插入顺序倒转过来的时候

改动一下标准Trie数的插入函数就能够了:

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. const int MAX_NODE = 100001;
  5. const int MAX_WORD = 11;
  6. const int ARR_SIZE = 10;
  7.  
  8. struct Node
  9. {
  10. int n;
  11. Node *arr[ARR_SIZE];
  12. };
  13.  
  14. void clearNode(Node *p)
  15. {
  16. p->n = 0;
  17. for (int i = 0; i < ARR_SIZE; i++)
  18. {
  19. p->arr[i] = NULL;
  20. }
  21. }
  22.  
  23. Node pool[MAX_NODE];
  24. int poolId;
  25.  
  26. bool insertTrie(Node *trie, char nums[])
  27. {
  28. Node *pCrawl = trie;
  29. int len = strlen(nums);
  30. for (int i = 0; i < len; i++)
  31. {
  32. int j = nums[i] - '0';
  33. //推断1: 情况: Trie有31199,插入311
  34. if (i + 1 == len && pCrawl->arr[j]) return false;//注意这个easy遗忘条件
  35. if (!pCrawl->arr[j])
  36. {
  37. pCrawl->arr[j] = &pool[poolId++];
  38. clearNode(pCrawl->arr[j]);
  39. }
  40. pCrawl = pCrawl->arr[j];
  41. if (pCrawl->n) return false;
  42. }
  43. for (int i = 0; i < ARR_SIZE; i++)
  44. {//推断2: 情况: Trie有31199,插入311,和推断1是一样的,删除一个也可。
  45.  
  46. if (pCrawl->arr[i]) return false;
  47. }
  48. pCrawl->n++;
  49. return true;
  50. }
  51.  
  52. int main()
  53. {
  54. int T, n;
  55. scanf("%d", &T);
  56. char word[MAX_WORD];
  57. Node *trie = &pool[0];
  58. while (T--)
  59. {
  60. clearNode(trie);
  61. poolId = 1;
  62. scanf("%d", &n);
  63. getchar();
  64. bool consistent = true;
  65. for (int i = 0; i < n; i++)
  66. {
  67. gets(word);
  68. if (consistent)
  69. {
  70. consistent = insertTrie(trie, word);
  71. }
  72. }
  73. if (consistent) puts("YES");
  74. else puts("NO");
  75. }
  76. return 0;
  77. }

POJ 3630 Phone List Trie题解的更多相关文章

  1. POJ 3630 Phone List | Trie 树

    题目: 给定 n 个长度不超过 10 的数字串,问其中是否存在两个数字串 S, T ,使得 S 是 T 的前缀.多组数据,数据组数不超过 40. 题解: 前缀问题一般都用Trie树解决: 所以跑一个T ...

  2. poj 3630 Phone List trie树

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

  3. poj 2431 Expedition 贪心 优先队列 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=2431 题解 朴素想法就是dfs 经过该点的时候决定是否加油 中间加了一点剪枝 如果加油次数已经比已知最少的加油次数要大或者等于了 那么就剪 ...

  4. poj 1064 Cable master 二分 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=1064 题解 二分即可 其实 对于输入与精度计算不是很在行 老是被卡精度 后来学习了一个函数 floor 向负无穷取整 才能ac 代码如下 ...

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

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

  6. POJ 3630 trie树

    Phone List Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26559 Accepted: 8000 Descripti ...

  7. Phone List POJ 3630 Trie Tree 字典树

    Phone List Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29416   Accepted: 8774 Descr ...

  8. POJ 3630 Phone List(字符串前缀重复)题解

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

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

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

随机推荐

  1. 怎样配置PHP环境和安装Zendstdio编辑器

    想学习PHP好久了.苦于环境配置不好,一直感觉无从下手. 在网上找了个视频: 李炎恢PHP教程 第一章前3节给出了具体的配置的方法,即安装Apache和Zendstudio 10.5仅仅须要照着视频做 ...

  2. Java-对象多态性

    class A {  public void fun1()  {   System.out.println("<----A------>");  }  public v ...

  3. 转载【浅谈ThreadPool 线程池】

    浅谈ThreadPool 线程池 http://www.cnblogs.com/xugang/archive/2010/04/20/1716042.html

  4. Xamarin.forms 自定义tabview控件

    一 问题描述 forms本身ui代码是翻译为平台原生代码,forms按照xaml技术进行对android和ios两种ui模型进行公共抽象出了几种page和view,在空杯博客已经有详细介绍 http: ...

  5. Nginx设置alias实现虚拟目录 alias与root的用法区别

    Nginx 貌似没有虚拟目录的说法,因为它本来就是完完全全根据目录来设计并工作的.如果非要给nginx安上一个虚拟目录的说法,那就只有alias标签比较"像",干脆来说说alias ...

  6. TMsgThread, TCommThread -- 在delphi线程中实现消息循环(105篇博客,好多研究消息的文章)

    在delphi线程中实现消息循环 在delphi线程中实现消息循环 Delphi的TThread类使用很方便,但是有时候我们需要在线程类中使用消息循环,delphi没有提供.   花了两天的事件研究了 ...

  7. 14.2.2 InnoDB Multi-Versioning InnoDB 多版本

    14.2.2 InnoDB Multi-Versioning InnoDB 多版本: InnoDB 是一个多版本的存储引擎: 它保留信息关于改变数据的老版本,为了支持事务功能 比如并发和回滚. 这些信 ...

  8. 单页应用Scrat实践

    单页应用Scrat实践 1.开始 随着前端工程化深入研究,前端工程师现在碉堡了,甚至搞了个自己的前端网站http://div.io/需要邀请码才能注册,不过里面的技术确实牛.距离顶级的前端架构,目前博 ...

  9. 无锁队列--基于linuxkfifo实现

    一直想写一个无锁队列,为了提高项目的背景效率. 有机会看到linux核心kfifo.h 原则. 所以这个实现自己仿照,眼下linux我们应该能够提供外部接口. #ifndef _NO_LOCK_QUE ...

  10. 部署到Linux使用VS Code 开发.NET Core 应用程序

    使用VS Code 开发.NET Core 应用程序 部署到Linux 跨平台 使用VS Code 开发.NET Core 应用程序 部署到Linux 跨平台. 前面讲解了VSCode开发调试 .NE ...