poj 2001 Shortest Prefixes(字典树)
题目链接:http://poj.org/problem?id=2001
思路分析:
在Trie结点中添加数据域childNum,表示以该字符串为前缀的字符数目;
在创建结点时,路径上的所有除叶子节点以外的结点的childNum增加1,叶子结点的childNum设置为1;
在查询某个结点的最短前缀时:
(1)若查找路径上的所有结点的childNum大于1,表明该字符串的最短路径为其自身;
(2)若查找路径上存在结点的childNum等于1,表明查找的字符串是唯一以该字符串为前缀的字符串;
代码如下:
#include <iostream>
#include <cstdlib> const int MAXN = ;
const int N = + ;
const int M = + ;
char dic[N][M];
struct Trie
{
int childNum;
Trie *child[MAXN];
Trie()
{
for (int i = ; i < MAXN; ++ i)
child[i] = NULL;
childNum = ;
}
}; Trie *root = NULL;
void insert(char *word)
{
Trie *cur = root;
int len = strlen(word); for (int i = ; i < len; ++ i)
{
int id = word[i] - 'a'; if (cur->child[id] == NULL)
cur->child[id] = new Trie;
else
cur->child[id]->childNum++;
cur = cur->child[id];
}
} int findShortestPrefixes(char *word)
{
int i, len = strlen(word);
Trie *cur = root; for (i = ; i < len; ++ i)
{
int id = word[i] - 'a'; cur = cur->child[id];
if (cur->childNum <= )
return i;
}
return -;
} int main()
{
int count = ; root = new Trie;
while (scanf("%s", dic[count]) != EOF)
insert(dic[count++]); for (int j = ; j < count; ++ j)
{
int ans = findShortestPrefixes(dic[j]); if (ans == -)
printf("%s %s\n", dic[j], dic[j]);
else
{
printf("%s ", dic[j]);
dic[j][ans + ] = '\0';
printf("%s\n", dic[j]);
}
} return ;
}
poj 2001 Shortest Prefixes(字典树)的更多相关文章
- POJ 2001 Shortest Prefixes(字典树)
题目地址:POJ 2001 考察的字典树,利用的是建树时将每个点仅仅要走过就累加.最后从根节点開始遍历,当遍历到仅仅有1次走过的时候,就说明这个地方是最短的独立前缀.然后记录下长度,输出就可以. 代码 ...
- poj 2001 Shortest Prefixes(字典树trie 动态分配内存)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15610 Accepted: 673 ...
- poj 2001:Shortest Prefixes(字典树,经典题,求最短唯一前缀)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12731 Accepted: 544 ...
- POJ 2001 Shortest Prefixes 【 trie树(别名字典树)】
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15574 Accepted: 671 ...
- POJ 2001 Shortest Prefixes(字典树活用)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 21651 Accepted: 927 ...
- OpenJudge/Poj 2001 Shortest Prefixes
1.链接地址: http://bailian.openjudge.cn/practice/2001 http://poj.org/problem?id=2001 2.题目: Shortest Pref ...
- POJ 2001 Shortest Prefixes (Trie)
题目链接:POJ 2001 Description A prefix of a string is a substring starting at the beginning of the given ...
- poj 2001 Shortest Prefixes trie入门
Shortest Prefixes 题意:输入不超过1000个字符串,每个字符串为小写字母,长度不超过20:之后输出每个字符串可以简写的最短前缀串: Sample Input carbohydrate ...
- poj2001 Shortest Prefixes(字典树)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 21642 Accepted: 926 ...
随机推荐
- J - 今年暑假不AC
J - 今年暑假不AC Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...
- Spring——自定义属性编辑器+Bean的生存范围+Bean的生命周期
一.自定义属性编辑器(一个类): 步骤: 1.写一个类,这个类继承PropertyEditorSupport. 2.重写setAsText()方法. 3.在bean.xml文件中添加属性编辑器的bea ...
- 【转】jsp 表单form传值
写的很好,看到了忍不住不转啊,希望可以分享一下~~ 转载自http://blog.csdn.net/anmei2010/article/details/4140216 页面间链接和数据传递的三种方式 ...
- “-Xmx1024m -Xms1024m -Xmn512m -Xss256k”——Java运行参数(转)
JVM的堆的内存, 是通过下面面两个参数控制的 -Xms 最小堆的大小, 也就是当你的虚拟机启动后, 就会分配这么大的堆内存给你 -Xmx 是最大堆的大小 当最小堆占满后,会尝试进行GC,如果GC之后 ...
- RedHat升级内核成功
升级前 uname -aLinux localhost.localdomain 2.6.18-194.el5 #1 SMP Tue Mar 16 21:52:39 EDT 2010 x86_64 x8 ...
- hdu 2563 统计问题
统计问题 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissi ...
- Tempter of the Bone(dfs奇偶剪枝)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- 30天自制操作系统第八天学习笔记(u盘软盘双启动版本)
暑假学习小日本的那本书:30天自制操作系统 qq交流群:122358078 ,更多学习中的问题.资料,群里分享 environment:开发环境:ubuntu 第八天的学习思考: 关于鼠标是怎么 ...
- HDU1250:Hat's Fibonacci
Problem Description A Fibonacci sequence is calculated by adding the previous two members the sequen ...
- typedef使用
1.利用typedef定义函数指针 代码简化,促进跨平台开发 typedef行为有点类似#define 宏,用其实际类型替代同义字. 不同点:typedef 在编译时被解释,因此让编译器来 应付超 ...