博客
关于我
Shortest Prefixes(trie树唯一标识)
阅读量:615 次
发布时间:2019-03-13

本文共 2323 字,大约阅读时间需要 7 分钟。

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

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

carbohydratecartcarburetorcaramelcariboucarboniccartilagecarboncarriagecartoncarcarbonate

Sample Output

carbohydrate carbohcart cartcarburetor carbucaramel caracaribou caricarbonic carbonicartilage carticarbon carboncarriage carrcarton cartocar carcarbonate carbona 题解:让找唯一能确定一个单词的前缀;我们想到当唯一确定的时候必定word[k]是1;那么很好解决了 代码:
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #define mem(x,y) memset(x,y,sizeof(x)) 8 using namespace std; 9 typedef long long LL;10 const int MAXN=10100;11 char s[1010][30];12 int ch[MAXN][30],word[MAXN];13 int sz;14 void initial(){15 sz=1;16 mem(word,0);17 }18 void insert(char *s){19 int len=strlen(s);20 int j,k=0;21 for(int i=0;i

 

转载地址:http://nueaz.baihongyu.com/

你可能感兴趣的文章
python中的序列化
查看>>
msfvenom的使用&免杀&外网渗透
查看>>
HTTP/2 协议详解
查看>>
2018年3月最新的Ubuntu 16.04.4漏洞提权代码
查看>>
亚马逊aws文档语法错误
查看>>
spark概述
查看>>
JavaScript 知识梳理[一] 变量类型,浅拷贝,深拷贝
查看>>
java.security.InvalidKeyException: Illegal key size
查看>>
Linux kernel pwn --- CSAW2015 StringIPC
查看>>
2020-05-31 py执行时超时控制
查看>>
双链表相加问题
查看>>
配置jdk的环境变量
查看>>
懒人HTML5笔记-1
查看>>
ThreadLocal源码分析解密
查看>>
编译android源代码(aosp)
查看>>
IDEA 找不到 Persistence窗口解决办法
查看>>
vagrant启动时提示 mount: unknown filesystem type 'vboxsf'
查看>>
维基百科之AndroidRoot
查看>>
C++ Primer Plus读书笔记:循环读取(错误处理)
查看>>
skimage与cv2 安装失败的解决办法
查看>>