描述


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. Ext.Net学习笔记12:Ext.Net GridPanel Filter用法

    Ext.Net学习笔记12:Ext.Net GridPanel Filter用法 Ext.Net GridPanel的用法在上一篇中已经介绍过,这篇笔记讲介绍Filter的用法. Filter是用来过 ...

  2. React-Native牛刀小试仿京东砍啊砍砍到你手软

    React-Native牛刀小试仿京东砍啊砍砍到你手软 React-Native基础教程 *React-Native基础篇作者git *React-Native官方文档 *Demo 几个月前faceb ...

  3. 我眼中真正优秀的CTO

    该文转自“肉饼铺子”.作者robbin是前JavaEye网站的创始人,TOPITCLUB互联网俱乐部发起人.  原文链接 现在进入正题,最近几个月,不断有人找我推荐CTO人选,这两年互联网创业和创投实 ...

  4. 图像储存容器Mat[OpenCV 笔记11]

    IplImage 与 Mat IplImage是OpenCV1中的图像存储结构体,基于C接口创建.在退出之前必须release,否则就会造成内存泄露.在一些只能使用C语言的嵌入式系统中,不得不使用. ...

  5. .net ajax式上传文件

    今天在这里介绍一下ajax上传文件.其实也不算是真的使用xmlhttprequest上传,只是使用了iframe实现了无刷新上传而已,最多也只算 是仿ajax上传文件.然而网上关于使用xmlhttpr ...

  6. jquery放大镜插件与样式

    这是放大镜插件链接,我已经上传到我博客http://files.cnblogs.com/valiant1882331/%E6%94%BE%E5%A4%A7%E9%95%9C%E6%8F%92%E4%B ...

  7. WinForm聊天室

    前几天开始学Socket编程,跟着老师一点一点的做.最后做了一个WinForm版的小聊天室.这个聊天室的客户端和服务端都只是在本机上运行. 这里我首先和大家谈谈我对聊天室的一点理解,聊天室其实是服务端 ...

  8. 安装 SQL Server 2012 的硬件和软件要求(官方全面)

    以下各节列出了安装和运行 SQL Server 2012 的最低硬件和软件要求. 有关 SharePoint 集成模式下 Analysis Services 的要求的详细信息,请参阅硬件和软件要求(S ...

  9. C# ADO.NET操作数据库 SqlHelp.cs类

    刚开始练习ADONET的时候,练习的一个SQLHelp.cs  数据库操作类,很简单,但是也很实用 using System; using System.Collections.Generic; us ...

  10. 查看用户列表在Linux

    Linux下查看用户列表   原文地址:http://xiaod.in/read.php?77 俺的centos vps上面不知道添加了多少个账户,今天想清理一下,但是以前还未查看过linux用户列表 ...