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. js关于cookie的各种方法

    //删除cookiedelCookie("GroupName");//s20是代表20秒//h是指小时,如12小时则是:h12//d是天数,30天则:d30setCookie(&q ...

  2. Java 线程生命周期

    |作者:RexFang |出处:http://www.cnblogs.com/rexfang/ |关于作者:Java 程序员一枚 |版权:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此 ...

  3. SourceTree Win10 安装过程及配置

    SourceTree 是一款拥有可视化界面的项目版本控制软件,适用于git项目管理,同时它集成了 git flow 工作流程,对于不熟悉 git 命令的初学者来说,可以通过 SourceTree 快速 ...

  4. leetcode: 链表2

    1. copy-list-with-random-pointer(拷贝一个带随机指针的链表) A linked list is given such that each node contains a ...

  5. Altium_Designer-怎么将“原理图的更改”更新到“pcb图”?

    打开原理图,直击菜单栏>>Design,选择第一项,>>Update PCB Document...在弹出的对话框里面选择执行更改即可将原理图更新到工程下面对应的PCB.也可以 ...

  6. wcf 的小介绍

    http://www.cnblogs.com/scottckt/archive/2010/10/15/1852136.html

  7. 解决cdh4.5.0下 MAP任务看不到状态

    参考 http://qnalist.com/questions/772595/yarn-jobhistory-service 在mapreduce-site.xml中添加 <property&g ...

  8. void和void*指针的一些理解

    void 和 void* 指针分别表示无类型和无类型指针. void 的作用是限制: 1,函数无返回值. 2,函数无参数. 当函数的返还值无参数的时候一定要加上 void ,因为在缺省的状态下函数的返 ...

  9. 前端css之文本操作及块级元素和行内元素

    1.文本操作 1.1文本颜色(color) 颜色指定方式: 十六进制值 - 如: #FF0000 一个RGB值 - 如: RGB(255,0,0) 颜色的名称 - 如:  red 1.2水平对齐方式 ...

  10. 改变shape solid color

    <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http: ...