博客
关于我
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/

你可能感兴趣的文章
STM32驱动0.96寸oled液晶屏
查看>>
【Altium Designer21】工作栏中文解析
查看>>
[87]用secureCRT连接虚拟机中的Ubuntu系统,出现“远程主机拒绝连接”错误
查看>>
[206]如何解决python升级后yum报错
查看>>
[308]mongo使用工具复制数据库和表
查看>>
[319]使用Python-markdown将markdown转换成html
查看>>
[363]python中优雅的用法
查看>>
[496]urllib.request.urlretrieve()函数
查看>>
prometheus 语法初探
查看>>
加载配置文件
查看>>
a++ + ++a
查看>>
Android 布局优化之<include/><merge/>和 <ViewStub>
查看>>
Shell脚本防DNS攻击检测并删除肉机IP
查看>>
升级Centos7.5的默认Python版本至最新
查看>>
如何在VSCode中定制JSON的IntelliSense
查看>>
PAT乙级真题 1042 字符统计 C++实现
查看>>
PAT乙级真题 1091 N-自守数 C++实现
查看>>
现实生活中常用的动态路由—OSPF路由重分发
查看>>
傅里叶变换时域和频域之间的对应关系
查看>>
椭圆曲线的定义
查看>>