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. HTML5 实现拖拽

    如图 可以从第一个方框拖拽花色到第二个方框中. 也可以再拖动回来. 具体代码实现 index.html <!DOCTYPE HTML> <html> <head> ...

  2. java反射机制 + Method.invoke解释 getMethod + 反射理解

    功能: 通过读取另一个Dll去创建一个控件(Form,Button,TextBox,DataGridView),然后对当中一些属性进行检查. 创建控件的大致流程是,Assembly->Modul ...

  3. 如何debug ruby

    how to debug ruby: 1. 第一种方法,直接使用ruby内建的debug在命令行调试,这个个gdb或者pdb的命令差不多. ruby -r debug yourubyfile.rb 2 ...

  4. Java回调理解 (step by step)

    在网上搜索了很多篇关于java回调函数的文章,自己也来试了一下写了这篇博客,可能有些地方理解不到位,烦请各位大大指正. 在计算机程序设计中.回调函数.或简称回调.是指通过函数參数传递到其他代码的,某一 ...

  5. linux 软连接 硬连接

    1.Linux链接概念 Linux链接分两种,一种被称为硬链接(Hard Link),另一种被称为符号链接(Symbolic Link).默认情况下,ln命令产生硬链接. [硬连接] 硬连接指通过索引 ...

  6. codeforces 598A Tricky Sum

    题目链接:http://codeforces.com/contest/598/problem/A 题目分类:大数 题意:1到n 如果是2的次方则减去这个数,否则就加上这个数,求最后的结果是多少 题目分 ...

  7. C/C++头文件

    C/C++头文件 #include <assert.h> //设定插入点 #include <ctype.h> //字符处理 #include <errno.h> ...

  8. 利用未公开API获取终端会话闲置时间(Idle Time)和登入时间(Logon Time)

    利用未公开API获取终端会话闲置时间(Idle Time)和登入时间(Logon Time)作者:Tuuzed(土仔)   发表于:2008年3月3日23:12:38 版权声明:可以任意转载,转载时请 ...

  9. Lambda高手之路第二部分

    转http://www.cnblogs.com/lazycoding/archive/2013/01/06/2847579.html 闭包的影响 为了展示闭包的影响,我们看下面这个例子. var bu ...

  10. webmagic加上了注解支持

    今天有个网友在博客回帖,能不能用注解来写一个爬虫?想了想,因为Javaer总习惯结果有个对象Model(我在自己用的时候也是这样),ResultItems的key-value形式难免会有点麻烦,何不将 ...