题目链接: 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. C++_知识点_指针类型转换

    #include <iostream> using namespace std; int main(){ ] = {, , , , , , , , , }; int* p = (int*) ...

  2. js命名空间的使用

    js命名空间的使用: test.html 代码如下: <!DOCTYPE HTML><html lang="en-US"><head>    & ...

  3. The basic introduction to MIX language and machine

    reference: The MIX Computer, The MIX Introduction sets, The basic info storage unit in MIX computer ...

  4. android和Vitamio使用比较

    在开始接触udp组播的时候先使用的Vitamio,播放时候声音卡顿 画面也会出现卡顿,后来又使用了VLC,画面挺好,,但是声音卡顿.最后不断测试发现是由于设备底层驱动处理视频部分有问题,导致程序播出的 ...

  5. c# winform读取xml创建菜单

    动态创建菜单使得程序灵活性大大增加,本文根据读取xml文件中的配置菜单项来动态创建菜单,代码如下: using System; using System.Collections.Generic; us ...

  6. 【每周一译】愚蠢的指标:Java中使用最多的关键字

    此翻译纯属个人爱好,由于水平所限,翻译质量可能较低.网络上可能存在其它翻译版本,原文地址:http://blog.jooq.org/2013/08/26/silly-metrics-the-most- ...

  7. Hibernate 配置详解(12) 补充

    hibernate.hbm2ddl.import_files_sql_extractor 这个配置项用于补充这篇文章: http://blog.csdn.net/stefwu/article/deta ...

  8. Android实现左右滑动指引效果

    本文介绍Android中实现左右滑动的指引效果. 关于左右滑动效果,我在以前的一篇博文中提到过,有兴趣的朋友可以查看:http://www.cnblogs.com/hanyonglu/archive/ ...

  9. 远程登录阿里云上的MySQL

    近期对云和server之类的感兴趣,想要将自己的数据什么的保存到远端server.研究了阿里云和百度云.今天算是有点进步吧. 我在阿里云上申请了个免费的云server(ECS),非常可惜仅仅能用5天. ...

  10. web前端的发展态势

     以前 作为一个java程序员写的代码主要还是后台的代码,虽然开始的时候前后端都写,但是也是用别人造好的轮子来用,学学html,css,js,jquery,再找一个前端ui框架学学,上手之后我们就可以 ...