题目链接: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(字典树)的更多相关文章

  1. POJ 2001 Shortest Prefixes(字典树)

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

  2. poj 2001 Shortest Prefixes(字典树trie 动态分配内存)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15610   Accepted: 673 ...

  3. poj 2001:Shortest Prefixes(字典树,经典题,求最短唯一前缀)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12731   Accepted: 544 ...

  4. POJ 2001 Shortest Prefixes 【 trie树(别名字典树)】

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15574   Accepted: 671 ...

  5. POJ 2001 Shortest Prefixes(字典树活用)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21651   Accepted: 927 ...

  6. OpenJudge/Poj 2001 Shortest Prefixes

    1.链接地址: http://bailian.openjudge.cn/practice/2001 http://poj.org/problem?id=2001 2.题目: Shortest Pref ...

  7. POJ 2001 Shortest Prefixes (Trie)

    题目链接:POJ 2001 Description A prefix of a string is a substring starting at the beginning of the given ...

  8. poj 2001 Shortest Prefixes trie入门

    Shortest Prefixes 题意:输入不超过1000个字符串,每个字符串为小写字母,长度不超过20:之后输出每个字符串可以简写的最短前缀串: Sample Input carbohydrate ...

  9. poj2001 Shortest Prefixes(字典树)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21642   Accepted: 926 ...

随机推荐

  1. 使用x manager 连接Linux桌面

    /usr/bin/xterm -ls -display $DISPLAY 需要安装xterm 服务

  2. Android 电源系列小结s

    package com.ritterliu.newBatteryWidget; import android.app.Activity; import android.app.Service; imp ...

  3. 数组求最大最小值和排序java实现

    public class ArrayDemo05 { public static void main(String[] args) {     int list01[]={67,89,87,69,90 ...

  4. sqlplus命令手册

    show errorshow allshow usersqlplus show和set命令是两条用于维护SQLPlus系统变量的命令 : SQL> show all --查看所有系统变量值 SQ ...

  5. (Problem 92)Square digit chains

    A number chain is created by continuously adding the square of the digits in a number to form a new ...

  6. jquery.validate 一些技巧

    1.Validator.element() Validates a single element, returns true if it is valid, false otherwise. http ...

  7. mysql远程登录权限

    GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'WITH GRANT OPTION; FLUSH PRIVILEGES;

  8. BC第二场

    GT and sequence  Accepts: 385  Submissions: 1467  Time Limit: 2000/1000 MS (Java/Others)  Memory Lim ...

  9. ArcMAp对线要素进行平滑处(打断)

    一:工具简单介绍 -- ArcMAp10.1的高级编辑工具中提供了对线/面要素进行概括/平滑处理的工具. 概括工具.平滑工具分别例如以下:(首先得开启编辑状态 --- 才干够对要素的属性进行更改).选 ...

  10. JavaScipt实现倒计时方法总结

    JavaScript中提供了两种实现计时.延时的方法,分别如下: 一. t = setTimeout(“function()", millisecond) 与 clearTimeout(t) ...