Shortest Prefixes

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 18687   Accepted: 8087

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 题目大意:给出一堆字符串,给每一个字符串找最小的前缀,让所有的前缀都不重复。
字典树询问时,找到一个val为1的说明这个点只有他自己一个,所以到此结束。
注意数组的大小
 #include<cstdio>
#include<cstring>
#define MAXN 100100 char s[][]; struct Trie
{
int ch[MAXN][];
int val[MAXN];
int size;
Trie()
{
size = ;
memset(ch,,sizeof(ch));
memset(val,,sizeof(val));
}
int id(char c)
{
return c-'a';
}
void Ins(char* s)
{
int u = , len = strlen(s);
for (int i=; i<len; ++i)
{
int c = id(s[i]);
if (!ch[u][c]) ch[u][c] = size++;
u = ch[u][c];
val[u]++;
}
}
void Find(char* s)
{
int u = , len = strlen(s);
for (int i=; i<len; ++i)
{
int c = id(s[i]);
u = ch[u][c];
printf("%c",s[i]);
if (val[u]==) return ;
}
}
}t; int main()
{
int p = ;
while (scanf("%s",s[++p])!=EOF)
{
t.Ins(s[p]);
}
for (int i=; i<=p; ++i)
{
printf("%s ",s[i]);
t.Find(s[i]);
printf("\n");
}
return ;
}

 

poj2001Shortest Prefixes(trie)的更多相关文章

  1. POJ2001Shortest Prefixes(Trie树)

    传送门 题目大意:求最短唯一前缀 题解:Trie树 把单词一个个插入,每个字母节点v[]++;然后输出时输出到v[]为1的点, v[]=1说明只有这个单词经过. 代码 : #include<io ...

  2. poj2001 Shortest Prefixes (trie)

    读入建立一棵字母树,并且每到一个节点就增加这个节点的覆盖数. 然后再重新扫一遍,一旦碰到某个覆盖数为1就是这个单词的最短前缀了. 不知为何下面的程序一直有bug……不知是读入的问题? type nod ...

  3. 【python】Leetcode每日一题-前缀树(Trie)

    [python]Leetcode每日一题-前缀树(Trie) [题目描述] Trie(发音类似 "try")或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的 ...

  4. LA3942-Remember the Word(Trie)

    题意: 有s个不同的单词,给出一个长字符串把这个字符串分解成若干个单词的连接(可重复使用),有多少种分解方法 分析: dp[i]表示i开始的字符串能分解的方法数 dp[i]=sum(dp[i+len( ...

  5. HDU 1671 Phone List (Trie)

    pid=1671">Phone List Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  6. codeforces 380A Sereja and Prefixes (递归)

    题目: A. Sereja and Prefixes time limit per test 1 second memory limit per test 256 megabytes input st ...

  7. hdu 1251 统计难题 (字典树(Trie)<PS:C++提交不得爆内存>)

    统计难题Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submis ...

  8. Shortest Prefixes(trie树唯一标识)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15948   Accepted: 688 ...

  9. POJ2001 Shortest Prefixes (Trie树)

    直接用Trie树即可. 每个节点统计经过该点的单词数,遍历时当经过的单词数为1时即为合法的前缀. type arr=record next:array['a'..'z'] of longint; w: ...

随机推荐

  1. Eclipse: 导入项目乱码问题解决

    1.编码不对 a.对某文件或某工程更改编码: 鼠标移到工程名或文件名,右键->Properties->Resource->Text file enCoding ->更改编码(G ...

  2. python3绘图示例6-2(基于matplotlib,绘图流程介绍及设置等)

    #!/usr/bin/env python# -*- coding:utf-8 -*- import os import numpy as npimport matplotlib as mpltfro ...

  3. Java运行机制及相关术语

    JVM java虚拟机(Java Virtual Machine)JVM可以实现java程序的夸平台运行,即运行的操作平台各不相同 JVM基本原理 java运行机制 编译型语言(如C.C++) 源文件 ...

  4. struts2.3.4.1转换成eclipse项目的过程

    1.在本地安装配置maven. 1.1.从maven官网下载,官网地址:http://maven.apache.org/download.cgi 1.2.配置maven环境变量       例如,我的 ...

  5. cesium 加载kml polygon和mark(贴地形terrain效果)

    key code: var options = { camera : viewer.scene.camera, canvas : viewer.scene.canvas, clampToGround: ...

  6. 【BZOJ4555】[TJOI2016&HEOI2016] 求和(NTT)

    点此看题面 大致题意: 计算\(\sum_{i=0}^n\sum_{j=0}^iS(i,j)*2^j*(j!)\),其中\(S\)为第二类斯特林数. 推式子 首先让我们来推一波式子: 因为当\(i&l ...

  7. telegram汉化和代理

    telegram在Ubuntu18.04的应用商店中可以一键下载. 1.注册:用国内手机号即可,就是验证码可能很慢. 2.汉化:关注zh-CN 频道,在点击其中的安装链接即可. 3.代理: 如果你使用 ...

  8. GNOME keyring [(null)] 的密码:

    在ubuntu下执行svn checkout命令时,总是报下面的错误: GNOME keyring [(null)] 的密码:svn: 方法 OPTIONS 失败于 “http://xxxxxxxx/ ...

  9. 离散外微积分(DEC:Discrete Exterior Calculus)基础

    原文链接 “若人们不相信数学简单,只因为他们未意识到生命之复杂.”——Johnvon Neumann DEC主要讨论离散情况下的外积分,它在计算机领域有重要用途.我们知道,使用计算机来处理几何图形的时 ...

  10. Python 2.x 和 3.x的区别

    Python有两个版本,2.x 和 3.x ,两个版本不兼容,3.x 不不考虑对2.x代码的向后兼容. 在3.x中,一些语法,内建函数和对象的行为都有所调整. 大部分的python库都支持 pytho ...