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题解的更多相关文章

  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. ext4 delalloc相关

    ext4文件系统delayed allocation相关研究 最近在一个项目上测试录音时,发现有丢数据的现象.通过串口发现打出了很多overrun的log. overrun是驱动层给上层应用的一个通知 ...

  2. 用XCA(X Certificate and key management)可视化程序管理SSL 证书(3)--创建自己定义的凭证管理中心(Certificate Authority)

    在第"用XCA(X Certificate and key management)可视化程序管理SSL 证书(2)---创建证书请求"章节中,我们介绍了怎样用XCA创建SSL证书请 ...

  3. Swift - 类型嵌套(以扑克牌结构体为例)

    类型嵌套,简单来说实在一个类型中包含另外一个类型.我们拿一副扑克来说明. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 //类 ...

  4. Qt制作应用插件

    在Qt下,插件有两种形式,一种是用于QtCreator下,扩展IDE功能.另一种是用于扩展开发者的应用.本文要讲的是后者. 定义一个纯虚类作为插件接口 #include <QtPlugin> ...

  5. NIO框架之MINA源码解析(转)

    http://blog.csdn.net/column/details/nio-mina-source.html http://blog.csdn.net/chaofanwei/article/det ...

  6. cct软件测试

    <全国计算机等级考试三级教程:软件测试技术(2016年版)>根据教育部考试中心制订的<全国计算机等级考试三级软件测试技术考试大纲(2013年版)>编写而成.主要内容包括软件测试 ...

  7. C文件操作的语言fgets()

        谈fgets(..)功能.     原型  char *  fgets(char * s, int n,FILE *stream);     參数:          s: 字符型指针.指向存 ...

  8. linux kernel的函数与抽象层

    在数学领域,函数是一种关系,这种关系使一个集合里的每一个元素对应到另一个(可能相同的)集合里的唯一元素. 在C语言中函数也有这种联系.自变量影响着因变量. 在linux内核驱动编程经常会有抽象层的概念 ...

  9. Delphi VMT的前世今生(研究一下D7的VMT表结构)

    主要是TObject那些虚函数,到底放在了哪里?

  10. [Android学习笔记]设置Activity方向

    1.设置Activity方向 在AndroidMainfest.xml里设置Activity默认方向 <activity android:name=".myActivity" ...