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. $("#Upfile").MultiFile();

    Jquery的multifile 1.多文件上传: 2.如上几个验证不重复,和限制上传数量的验证显示的是英文,改成中文文本时,如果不用国标解码,到时候提示框会出现乱码现象.所以一般需要中文显示的时候, ...

  2. centos6.5_64bit_tomcat7开机自启

    一.创建tomcat脚本 vim /etc/init.d/tomcat 将下面的内容拷到脚本里面 =================================================== ...

  3. Python基础学习之字符串(1)

    字符串 由字符组成的序列,即字符串. 1.基本字符串操作 所有标准的序列操作(索引.切片.乘法.判断成员资格.求长度.取最小值和最大值)对字符串同样适用: >>> website=' ...

  4. Win10技巧:使用“照片”应用剪辑视频、添加特效

    Win10内置了很多实用的应用,你不仅可以通过“Win键+G”快速录制电脑屏幕,如软件操作.游戏界面等,你还可以利用“照片”应用来对视频进行快速的剪辑,把录制前后多余的内容去除,同时你也可以对游戏中的 ...

  5. Linux远程桌面(二)

    上一篇远程桌面采用的独立服务配置不适用于过多用户,这一篇采用超级Internet服务器搭建vnc服务可以解决多用户问题.  vnc之xinetd服务搭建配置 Linux远程桌面(一):vnc之独立服务 ...

  6. git/github初级运用自如(转自:虫师)

    注:本文来源于 虫师博客(http://www.cnblogs.com/fnng/archive/2012/01/07/2315685.html) ,内容详尽,真实有用. 另:发一个github使用教 ...

  7. Jmeter入门6 参数化—CSV Data Set Config 通过文件导入数据

    线程组循环次数大于1的时候,请求里每次提交的数据都相同.有的系统限制了不能提交相同数据,我们通过 CSV Data Set Config 加载csv文件数据. 1 创建一个文本文件,输入参数值保存为. ...

  8. hdu-2838 Cow Sorting---逆序对的花费

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2838 题目大意: 就是求将之前的排列变成一个递增的排列,每交换两个数的代价为两个数的和,求变成递增的 ...

  9. Codeforces Round #404 (Div. 2) ABC

    A. Anton and Polyhedrons Anton's favourite geometric figures are regular polyhedrons. Note that ther ...

  10. poj 2057 树形DP,数学期望

    题目链接:http://poj.org/problem?id=2057 题意:有一只蜗牛爬上树睡着之后从树上掉下来,发现后面的"房子"却丢在了树上面, 现在这只蜗牛要求寻找它的房子 ...