描述


http://poj.org/problem?id=2001

给出一组单词,求每个单词的最小唯一前缀.

最小唯一前缀:该前缀不能是其他单词的前缀,并且最小,如果不存在,则为该单词本身.

Shortest Prefixes
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 16921   Accepted: 7338

Description

A prefix of a string is a substring starting at the beginning of the given string. The prefixes of "carbon" are: "c", "ca", "car", "carb", "carbo", and "carbon". Note that the empty string is not considered a prefix in this problem, but every non-empty string is considered to be a prefix of itself. In everyday language, we tend to abbreviate words by prefixes. For example, "carbohydrate" is commonly abbreviated by "carb". In this problem, given a set of words, you will find for each word the shortest prefix that uniquely identifies the word it represents.

In the sample input below, "carbohydrate" can be abbreviated to
"carboh", but it cannot be abbreviated to "carbo" (or anything shorter)
because there are other words in the list that begin with "carbo".

An exact match will override a prefix match. For example, the prefix
"car" matches the given word "car" exactly. Therefore, it is understood
without ambiguity that "car" is an abbreviation for "car" , not for
"carriage" or any of the other words in the list that begins with "car".

Input

The
input contains at least two, but no more than 1000 lines. Each line
contains one word consisting of 1 to 20 lower case letters.

Output

The
output contains the same number of lines as the input. Each line of the
output contains the word from the corresponding line of the input,
followed by one blank space, and the shortest prefix that uniquely
(without ambiguity) identifies this word.

Sample Input

carbohydrate
cart
carburetor
caramel
caribou
carbonic
cartilage
carbon
carriage
carton
car
carbonate

Sample Output

carbohydrate carboh
cart cart
carburetor carbu
caramel cara
caribou cari
carbonic carboni
cartilage carti
carbon carbon
carriage carr
carton carto
car car
carbonate carbona

Source

分析


唯一前缀就要求前缀的末位置只出现过一次,所以在Trie中insert的时候顺便记下每个点经过的次数.查找时一直找到第一个只出现了一次的点,或者查完整个单词.

p.s.打打Trie模板,已经忘干净了.

 #include <cstdio>
#include <cstring>
using namespace std; const int type=; struct Trie{
struct node{
node* next[type];
int num;
node(){
num=;
for(int i=;i<type;i++) next[i]=NULL;
}
}*root;
Trie(){ root=new node; }
inline int id(char c) { return c-'a'; }
void insert(char *c){
node* o=root;
while(*c){
int t=id(*c);
if(o->next[t]==NULL) o->next[t]=new node;
o=o->next[t];
o->num++;
c++;
}
}
void search(char *c){
node* o=root;
while(*c){
int t=id(*c);
o=o->next[t];
printf("%c",*c);
if(o->num==) return;
c++;
}
}
}tree;
char c[][]; int main(){
int cnt=;
while(scanf("%s",c[++cnt])!=EOF) tree.insert(c[cnt]);
for(int i=;i<=cnt;i++){
printf("%s ",c[i]);
tree.search(c[i]);
printf("\n");
}
return ;
}

POJ_2001_Shortest_Prefixes_(Trie)的更多相关文章

  1. 基于trie树做一个ac自动机

    基于trie树做一个ac自动机 #!/usr/bin/python # -*- coding: utf-8 -*- class Node: def __init__(self): self.value ...

  2. 基于trie树的具有联想功能的文本编辑器

    之前的软件设计与开发实践课程中,自己构思的大作业题目.做的具有核心功能,但是还欠缺边边角角的小功能和持久化数据结构,先放出来,有机会一点点改.github:https://github.com/chu ...

  3. [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

  4. hihocoder-1014 Trie树

    hihocoder 1014 : Trie树 link: https://hihocoder.com/problemset/problem/1014 题意: 实现Trie树,实现对单词的快速统计. # ...

  5. 【BZOJ-2938】病毒 Trie图 + 拓扑排序

    2938: [Poi2000]病毒 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 609  Solved: 318[Submit][Status][Di ...

  6. Poj The xor-longest Path 经典题 Trie求n个数中任意两个异或最大值

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5646   Accepted: 1226 Description In an ...

  7. 二分+DP+Trie HDOJ 5715 XOR 游戏

    题目链接 XOR 游戏 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  8. 【hihoCoder】1036 Trie图

    题目:http://hihocoder.com/problemset/problem/1036 给一个词典dict,词典中包含了一些单词words.要求判断给定的一个文本串text中是否包含这个字典中 ...

  9. 萌新笔记——C++里创建 Trie字典树(中文词典)(一)(插入、遍历)

    萌新做词典第一篇,做得不好,还请指正,谢谢大佬! 写了一个词典,用到了Trie字典树. 写这个词典的目的,一个是为了压缩一些数据,另一个是为了尝试搜索提示,就像在谷歌搜索的时候,打出某个关键字,会提示 ...

随机推荐

  1. Linq 与UnitOfWork

    submitchages(linq to sql)或者savechanges(ef)的次数是根据你操作方法的数量决定的,也即是:它只认识自己的提交语句(submtchanges,savechanges ...

  2. Core Data(数据持久化)

    Core Data可能是OS X和iOS中最容易被误解的框架之一了.为了帮助大家理解,我们将快速研究Core Data,来看一下它是关于什么的.为了正确使用Core Data, 有必要理解其概念.几乎 ...

  3. Javascript 迭代法实现数组多条件排序

    多条件排序可能有很多种思路,效率也各不相同,我的方法可能只适合自己用,毕竟目的是为了实现功能,所以采用了最笨的方法,不过效果还是很理想的,经过多次测试,6列1000行数据,平均排序时间大约是:28ms ...

  4. Eclipse 和 Intellij idea 快捷键的区别

    描述 Eclipse IntelliJ 代码补全 Ctrl+space ctrl+space 打开类或者接口 (两个IDE都支持使用“驼峰字符”前缀的方式来过滤查找列表,进而轻松完成搜索:比如:可以使 ...

  5. 【培训】交换机VLAN

    为了解决用交换机做LAN互联无法限制广播的问题,出现了VLAN技术,把一个LAN划分为多个逻辑的“LAN”-VLAN. VLAN技术将一个物理的LAN逻辑地划分为不同的广播域,每一个VLAN包含一组有 ...

  6. Sdut 2416 Fruit Ninja II(山东省第三届ACM省赛 J 题)(解析几何)

    Time Limit: 5000MS Memory limit: 65536K 题目描述 Haveyou ever played a popular game named "Fruit Ni ...

  7. 深度优化LNMP之Nginx [2]

    深度优化LNMP之Nginx [2]   配置Nginx gzip 压缩实现性能优化 1.Nginx gzip压缩功能介绍        Nginx gzuo压缩模块提供了压缩文件内容的功能,用户请求 ...

  8. 如何在Html的div+css中去除<li>标签前面小黑点,和ul、LI部分属性方法

    div是很多人做网站都会用到的,但在显示效果时前面总是会有一个小黑点,这个效果很多人不想要,但又不知到如何去除,然而我们可以用以下方法来清除. 1.在CSS中写入代码.找到相关性的CSS,在..li和 ...

  9. c#解决数据库用in的时候大于1000报错问题

    问题: //oracle数据库报错 ,,,,,,,.....); c#写了一个方法解决 /// <summary> /// 拼接sql /// </summary> /// & ...

  10. yum安装lamp环境

    装了好些次lamp环境了,都没好好总结下,现在总结下 ^ ^ 1.替换163的yum源 1.检查系统版本 cat /etc/redhat-releas   (我的版本是CentOS release 6 ...