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 ...
随机推荐
- Java学习之字符串练习
1.给定一个字符串数组.按照字典顺序进行从小到大的排序. * 思路: * 1,对数组排序.可以用选择,冒泡都行. * 2,for嵌套和比较以及换位. * 3,问题:以前排的是整数,比较用的比较运算符, ...
- mysql的触发器
删除触发器 drop TRIGGER 触发器名字; 查找库里面的所有触发器 SELECT * FROM information_schema.`TRIGGERS`;show triggers 触发器语 ...
- poj 3252 Round Numbers 数位dp
题目链接 找一个范围内二进制中0的个数大于等于1的个数的数的数量.基础的数位dp #include<bits/stdc++.h> using namespace std; #define ...
- python一些模块的exe安装包在windows的64位系统里识别不到已安装Python目录的解决方法
在windows里安装python一些模块时候,有时候源码安装比较困难,pip install也各种报错,这时候最喜欢用别人编译好的exe或者whl文件来安装,但是在windows的64位系统里,如果 ...
- Python数据分析扩展库
Anaconda和Python(x,y)都自带了下面的这些库. 1. NumPy 强大的ndarray和ufunc函数. import numpy as np xArray = np.ones((3, ...
- 《Pointers On C》读书笔记(第四章 语句)
1.空语句只包含一个分号,它本身并不执行任何任务,其适用的场合是语法要求出现一条完整的语句,但并不需要它执行任何任务. 2.C语言中并不存在专门的“赋值语句”,赋值就是一种操作,在表达式内进行.通过在 ...
- 批量添加target属性
<script> addTarget(); function addTarget(){ var oa=document.getElementsByTagName('a'); for(var ...
- karma note
karma 简介 karma不是一个测试框架,仅仅是启动了一个httpServer karma仅仅是一个跑测试的平台(runner), 我们可以使用各种测试框架, 如: Jasmine Mocha Q ...
- hibernate-search-5.1.1简易使用
系统要求java7和hibernate-core-4.3.8,此外还依赖如下jar包 使用demo如下: package com.ciaos.controller; import java.io.IO ...
- Swift中的UIKit重力学
前言: 重力学这个名词不论在哪个行业领域听起来似乎都非常高大上. 那么在Swift中的重力学是什么呢?那就是将我们移动端屏幕上毫无生命力的东西也置于万有引力中.使它们能够展现出好像真的因为引力而向下坠 ...