题目链接: 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. CM 0313 Review

    中午无聊看了贴吧,看到有人截图说CM有爱的故事.看到SE03CH13,觉得图有点印象,似乎我很是记得这一集.于是刚才看了一下,嗯,果然记得.是我头一次翻译美剧的时候,7年前. 剧情还可以吧,剧中的Ji ...

  2. poj2175

    鸣谢: http://www.cppblog.com/y346491470/articles/152317.html [题意]:一个城市有n座建筑物,每个建筑物里面有一些人,为了在战争爆发时这些人都可 ...

  3. Spring学习之常用注解(转)

    使用注解来构造IoC容器 用注解来向Spring容器注册Bean.需要在applicationContext.xml中注册<context:component-scan base-package ...

  4. latex如何把目录页的页码去掉?

    页眉的显示与关闭,清空,还有样式之间的切换,需要用到如下几个命令: \pagestyle  用于设置当前页以及后续页面的页眉显示情况(可称为页版式).中间页版式可由\thispagestyle命令来指 ...

  5. 基于JDK 8的Dubbo Admin

    在使用Dubbo  Admin的时候,一直报错,无法启动,因为Dubbo Admin使用的各种库相对是比较旧的,在JDK 8下,有些小问题 具体解决过程参考的以下链接 https://github.c ...

  6. cloneNode小结

    js原生API中有个cloneNode,还有一个可选的参数, true代表复制子节点,包括任何包裹在标签之间的东西,当然包括文本节点,也就是标签之间有什么,它就会不假思索的全部都克隆一份. false ...

  7. mysql的函数

  8. UVA - 1103Ancient Messages(dfs)

    UVA - 1103Ancient Messages In order to understand early civilizations, archaeologists often study te ...

  9. [ACM] POJ 2253 Frogger (最短路径变形,每条通路中的最长边的最小值)

    Frogger Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 24879   Accepted: 8076 Descript ...

  10. unix more命令

    [语法]: more   [-cdflrsuw]  [- 行数] [+ 行数] [+ / 模式 ] [ 文件 ... ] [说明]: 将文件显示在终端上.每次一屏,在左下部显示 --more--.若是 ...