题目链接: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. MyEclipse 2013 导入MyEclipse 9.0的EJB项目时,需要注意

    点击“next”按钮,出现下面的对话框: 再点击“next”按钮,出现下面的对话框:

  2. ##DAY9 UITabBarController

    ##DAY9 UITabBarController UIViewController的tabBarController UIViewController的tabBarItem #pragma mark ...

  3. BZOJ 1066: [SCOI2007]蜥蜴( 最大流 )

    结点容量..拆点然后随便写 --------------------------------------------------------------- #include<cstdio> ...

  4. MySQL 基础学习

    http://www.w3school.com.cn/sql/ 1.limit x,y 或 limit z  :选取从x开始的y条数据  或  选取最开始的 z条数据 , 2.like '%N%' : ...

  5. String的构造函数、析构函数和赋值函数

    编写类String的构造函数.析构函数和赋值函数 已知类String的原型为: class String { public: String(const char *str = NULL); // 普通 ...

  6. MySQL 创建用户与修改密码

    创建用户的 3 方法: 1 .create user userName identifed by 'yourPassword'; 2. grant select on *.* to userName@ ...

  7. issues about Facebook Login

    在学习The complete iOS 9 Developer Course - Build 18 Apps 中的Letture134-Facebook Login,需要整合(integrate)Pa ...

  8. Android Material Design 之 Toolbar的使用

    Material Design是谷歌提出来的最新ui设计规范,我想actionbar大家也许并不陌生,toolbar简单而言可以看做为actionbar的升级版,相比较actionbar而言,tool ...

  9. DateTimePicker——开源的Android日历类库

    Github托管地址:https://github.com/flavienlaurent/datetimepicker

  10. [VC6 console]调用API获取手机归属地

    为了完成作业,就偷个懒糊了个获取手机归属地的程序,.我原本写的是MFC版本的,但是由于MFC的代码不是很通用,加上我没有学MFC的时候看别人MFC代码只能干瞪眼,看不懂,所以便改成控制台版本的了.但这 ...