题目链接: http://poj.org/problem?id=3630

思路分析:

求在字符串中是否存在某个字符串为另一字符串的前缀:

即对于某个字符串而言,其是否为某个字符串的前缀,或存在某个其先前的字符串为其前缀;

(1)若该字符串为某个字符串前缀,则存在一条从根节点到该字符串的最后一个字符串的路径;

(2)若存在某个字符串为该字符串前缀,则在该字符串的查找路径中存在一条子路径,路径的最后的结点的endOfWord

标记为true,表示存在某个字符串为其前缀;

代码:

#include <iostream>
using namespace std; const int MAX_N = ;
struct Trie
{
bool endOfWord;
Trie *next[MAX_N];
Trie()
{
for (int i = ; i < MAX_N; ++i)
next[i] = NULL;
endOfWord = false;
}
};
int nodeCount = ;
Trie *root = NULL;
Trie memory[]; bool insertAndJudge(char *word)
{
Trie *cur = root, *next;
int len = strlen(word); for (int i = ; i < len; ++ i)
{
int k = word[i] - ''; if (cur->next[k] == NULL)
{
next = &memory[nodeCount++];
cur->next[k] = next;
cur = cur->next[k];
}
else
if (i == len - || cur->next[k]->endOfWord == true)
return false;
else
cur = cur->next[k];
}
cur->endOfWord = true;
return true;
} int main()
{
int t; cin >> t;
while (t--)
{
int n;
bool flag = true;
char word[]; nodeCount = ;
memset(memory, , sizeof(memory));
root = &memory[nodeCount++]; cin >> n;
while (n--)
{
scanf("%s", word);
if (flag) flag = insertAndJudge(word);
}
if (flag)
cout << "YES" << endl;
else
cout << "NO" << endl;
} return ;
}

poj 3630 Phone List(字典树)的更多相关文章

  1. Phone List POJ 3630 Trie Tree 字典树

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

  2. POJ 2001 Shortest Prefixes(字典树)

    题目地址:POJ 2001 考察的字典树,利用的是建树时将每个点仅仅要走过就累加.最后从根节点開始遍历,当遍历到仅仅有1次走过的时候,就说明这个地方是最短的独立前缀.然后记录下长度,输出就可以. 代码 ...

  3. nyoj 163 Phone List(动态字典树<trie>) poj Phone List (静态字典树<trie>)

    Phone List 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 Given a list of phone numbers, determine if it i ...

  4. poj 1204 Word Puzzles(字典树)

    题目链接:http://poj.org/problem?id=1204 思路分析:由于题目数据较弱,使用暴力搜索:对于所有查找的单词建立一棵字典树,在图中的每个坐标,往8个方向搜索查找即可: 需要注意 ...

  5. poj 1056 IMMEDIATE DECODABILITY 字典树

    题目链接:http://poj.org/problem?id=1056 思路: 字典树的简单应用,就是判断当前所有的单词中有木有一个是另一个的前缀,直接套用模板再在Tire定义中加一个bool类型的变 ...

  6. POJ 2408 - Anagram Groups - [字典树]

    题目链接:http://poj.org/problem?id=2408 World-renowned Prof. A. N. Agram's current research deals with l ...

  7. POJ 1816 - Wild Words - [字典树+DFS]

    题目链接: http://poj.org/problem?id=1816 http://bailian.openjudge.cn/practice/1816?lang=en_US Time Limit ...

  8. poj 2503:Babelfish(字典树,经典题,字典翻译)

    Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 30816   Accepted: 13283 Descr ...

  9. poj 2513 连接火柴 字典树+欧拉通路 好题

    Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 27134   Accepted: 7186 ...

随机推荐

  1. 软件开发常用Linux命令

    解压缩 tar -zxvf xxx.tar.gz 文件显示及查找常用于分析log //显示file中包含aaa的行 cat <file>|grep aaa 查看cpu memory基本信息 ...

  2. IOS 特定于设备的开发:基于加速计的滚动视图

    倾斜滚轮使用设备的内置加速计来控制在UIScrollView的内容周围移动.当用户调增设备时,材料会相应的下落,他不会把视图定位在屏幕上,而是把内容视图滚动到一个新的偏移位置. 创建这个界面的挑战在于 ...

  3. html 转 js 字符串

    看到一个牛人的博客  http://riny.net/lab/#tools_html2js 看了下他的代码  挺棒的 所依赖的两个库在这里 https://github.com/Bubblings/l ...

  4. OSG中的智能指针

    在OpenSceneGraph中,智能指针(Smart pointer)的概念指的是一种类的模板,它针对某一特定类型的对象(即Referenced类及其派生类)构建,提供了自己的管理模式,以避免因为用 ...

  5. JAVA并发,同步锁性能测试

    测试主要从运行时间差来体现,数据量越大,时间差越明显,例子如下: package com.xt.thinks21_2; /** * 同步锁性能测试 * * @author Administrator ...

  6. Win7下超级管理员创建普通权限任务

    已转至新的博客 http://www.raysoftware点击打开链接.cn/?p=49 项目中用到一个功能,Win7下超级管理员创建普通权限任务. 试了几种办法,例如获取资源管理器的Token,然 ...

  7. mongodb初体验

    最近关注大数据,自然会关注到nosql数据库,其中当然是mongodb. nosql数据库大多是k,v数据库,这也不是新鲜的名词了,berkerly DB已经存在很多年了,现在属于oracle. 具体 ...

  8. 51nod 1239 欧拉函数之和(杜教筛)

    [题目链接] https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1239 [题目大意] 计算欧拉函数的前缀和 [题解] 我们 ...

  9. Java图形化界面设计——布局管理器之CardLayout(卡片布局)

  10. PHP计划任务:如何使用Linux的Crontab执行PHP脚本

    我们的PHP程序有时候需要定时执行,我们可以使用ignore_user_abort函数或是在页面放置js让用户帮我们实现.但这两种方法都不太可靠,不稳定.我们可以借助Linux的Crontab工具来稳 ...